Quantcast
Channel: How to initialize static variables - Stack Overflow
Viewing all articles
Browse latest Browse all 11

Answer by Kornel for How to initialize static variables

$
0
0

PHP can't parse non-trivial expressions in initializers.

I prefer to work around this by adding code right after definition of the class:

class Foo {  static $bar;}Foo::$bar = array(…);

or

class Foo {  private static $bar;  static function init()  {    self::$bar = array(…);  }}Foo::init();

PHP 5.6 can handle some expressions now.

/* For Abstract classes */abstract class Foo{    private static function bar(){        static $bar = null;        if ($bar == null)            bar = array(...);        return $bar;    }    /* use where necessary */    self::bar();}

Viewing all articles
Browse latest Browse all 11

Trending Articles