Php static variable initialization

Possible duplicate:
Syntax error when defining an array as a class property

I am trying to do the following:

final class TestClass {
    public static $myvar = 10*10; //line 3
    //rest of code...
}

but I get this error: syntax error, unexpected '*', expecting ',' or ';' [line 3]

why is this impossible? Of course, if I change 10 * 10 to 100, everything will be fine. Is it allowed to run a static variable with math calculation? Impossible in any way?

+2
source share
4 answers

From php docs

PHP, ; . , (), , .

+9

, init ,

final class TestClass {
    public static $myvar = null; //line 3

    public static function init() {
    self::$myvar = 10*10;
    }
    //rest of code...
}

init,

TestClass::init();

+5

. ( ) , .

+2

/ . (protected $_value = 10;) (protected static $_arrr = array ( "key" = > "value" )).

You can create an az Initialize static method and a $ _isInitialized static field to check before reinitializing your class, but you will have to somehow call the Initialize method (in the constructor, the same factory implementation, etc.).

+2
source

All Articles