5
...

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.

Charles
  • 50,943
  • 13
  • 104
  • 142
Blake
  • 140
  • 9
  • 1
    Works fine for me -> http://codepad.org/EIorteTQ .... im guessing its a problem defining non-constant variable in a class – Manse Feb 10 '12 at 09:40
  • Have you tried to just remove the outer `( )`? Or just all `( )` on that line, as there is no need for them when doing multiplication... – Svish Feb 10 '12 at 09:42
  • I removed the access specifier public and it works – Poonam Feb 10 '12 at 09:43
  • [silly's answer](https://stackoverflow.com/a/9633940/1941213) to a similar question is useful in this regard. While it uses the same approach as [Michael Krelin's](https://stackoverflow.com/a/9225689), it shows a way to include the initialization logic within the class definition. – Alex Coventry Feb 22 '13 at 23:48

6 Answers6

8

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.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
deceze
  • 510,633
  • 85
  • 743
  • 889
3

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.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
3

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

pjumble
  • 16,880
  • 6
  • 43
  • 51
1

When you define variable in class, you cannot assign expression to it. (5 * (1024 * 1024)) is an expression. 6164480 is not.

Timur
  • 6,668
  • 1
  • 28
  • 37
0

This limitation no longer exists as of PHP 5.6

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
Jon
  • 428,835
  • 81
  • 738
  • 806
-3

Public is a declaration only used in objects. This is not an object, remove public and it's fine.

thenetimp
  • 9,487
  • 5
  • 29
  • 42