9123, 'MaxFileSize...">

Syntax error when defining an array as a class property

...

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, right from the class. I have a problem:, "unexpected ( on line 22"line 22 - MaxFileSize.

I don't see a problem with this, is this a limitation of Zend Engine? Or I'm blind.

+5
source share
7 answers

You cannot use inconsistent values ​​when initializing class properties in PHP versions earlier than 5.6.
They are initialized at compile time, in which PHP will not perform any calculations or execute any code. (5 * (1024 * 1024))is an expression that requires an assessment that you cannot do. Either replace it with a constant value 5242880, or do the calculation in __construct.

PHP 5.6, 2014 , " " , , .

+8

, , , .

, - :

class thingamajig {
    public static $aSettings;
};
thingamajig::$aSettings = array ( ... );

P.S. , , . .

+3

, . (5 * (1024 * 1024)) - . 6164480 .

+1
0

PHP 5.6

, , :

, / , PHP , .

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; ?>

:

4 The value of THREE is 3
0

Public is an ad that is used only in objects. This is not an object, delete the publication and this is normal.

-2
source

All Articles