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.

4
  • 1
    Works fine for me -> codepad.org/EIorteTQ .... im guessing its a problem defining non-constant variable in a class Commented Feb 10, 2012 at 9: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... Commented Feb 10, 2012 at 9:42
  • I removed the access specifier public and it works Commented Feb 10, 2012 at 9:43
  • silly's answer to a similar question is useful in this regard. While it uses the same approach as Michael Krelin's, it shows a way to include the initialization logic within the class definition. Commented Feb 22, 2013 at 23:48

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I wasn't aware of that. I'll move the operations to my construct.
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.

2 Comments

Indeed it is not all of the code, it was taken from a class file, I removed the surrounding code.
Yeah, read that now. At first it was too obvious to finish reading :)
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

Comments

1

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

2 Comments

Technically 6164480 is an expression as well, just a very simple one. :o)
Hmm... Yep, @deceze described it a lot more accurate :)
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

Comments

-3

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

2 Comments

The code was taken FROM an object, hence ignore the public keyword.
OK, well I did take it out and php -l came back with no errors, so the error is elsewhere even though it's reporting there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.