Reputation: 140
...
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.
Upvotes: 5
Views: 967
Reputation: 522587
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.
Upvotes: 8
Reputation: 437784
The new feature that enables the previously-disallowed syntax is called constant scalar expressions:
It is now possible to provide a scalar expression involving numeric and string literals and/or constants in contexts where PHP previously expected a static value, such as constant and property declarations and default function arguments.
class C { const THREE = TWO + 1; const ONE_THIRD = ONE / self::THREE; const SENTENCE = 'The value of THREE is '.self::THREE; public function f($a = ONE + self::THREE) { return $a; } } echo (new C)->f()."\n"; echo C::SENTENCE; ?>
The above example will output:
4 The value of THREE is 3
Upvotes: 0
Reputation: 6718
When you define variable in class, you cannot assign expression to it. (5 * (1024 * 1024))
is an expression. 6164480
is not.
Upvotes: 1
Reputation: 9830
Public is a declaration only used in objects. This is not an object, remove public and it's fine.
Upvotes: -3
Reputation: 16960
I assume what you're showing is actually a class property (because of the public keyword). Initialization of class properties in PHP must be constant.
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
http://www.php.net/manual/en/language.oop5.properties.php
Upvotes: 3
Reputation: 143279
I suspect this is not the whole code and this is a definition of a static variable inside a class, where you're quite limited in expressions and can't calculate a lot.
If I'm right, you may want to do something like that instead:
class thingamajig {
public static $aSettings;
};
thingamajig::$aSettings = array ( ... );
P.S. Sorry, I've just read your prose where you confirm it's a part of a class static variable. So you can't just ignore out-of-place keyword.
Upvotes: 3