Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

php - Syntax error while defining an array as a property of a class

...

public $aSettings = array(
  'BindHost' => "127.0.0.1",
  'Port' => 9123,
  'MaxFileSize' => (5 * (1024 * 1024)), // unexpected "(" here
  'UploadedURL' => "http://localhost",
  'UploadPath' => dirname(__FILE__) . "/upload",
  'UploadMap' => dirname(__FILE__) . "/uploads.object",
  'RegisterMode' => false
);

...

This is my code, straight from a class. The problem I have is the "unexpected ( on line 22", line 22 being MaxFileSize.

I can't see a problem with it, is this a Zend Engine limitation? Or am I blind.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot use non-constant values while initializing class properties in PHP versions earlier than 5.6.
These are initialized at compile time, at which PHP will do no calculations or execute any code. (5 * (1024 * 1024)) is an expression that requires evaluation, which you cannot do there. Either replace that with the constant value 5242880 or do the calculation in __construct.

PHP 5.6, introduced in 2014, allows "constant scalar expressions" wherein a scalar constant or class property can be initialized by an evaluated expression in the class definition rather than the constructor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...