Answer by David Spector for How to initialize static variables
In my case, I'm using both static and nonstatic class properties, and I might even have main program code referencing the static part of the class before defining the class. Since static portions of...
View ArticleAnswer by Buffalo for How to initialize static variables
In PHP 7.0.1, I was able to define this: public static $kIdsByActions = array( MyClass1::kAction => 0, MyClass2::kAction => 1);And then use it like this:MyClass::$kIdsByActions[$this->mAction];
View ArticleAnswer by Mambazo for How to initialize static variables
I use a combination of Tjeerd Visser's and porneL's answer.class Something{ private static $foo; private static getFoo() { if ($foo === null) $foo = [[ complicated initializer ]] return $foo; } public...
View ArticleAnswer by espaciomore for How to initialize static variables
best way is to create an accessor like this:/*** @var object $db : map to database connection.*/public static $db= null; /*** db Function for initializing variable. * @return object*/public static...
View ArticleAnswer by David Luhman for How to initialize static variables
Here is a hopefully helpful pointer, in a code example. Note how the initializer function is only called once.Also, if you invert the calls to StaticClass::initializeStStateArr() and $st = new...
View ArticleAnswer by diggie for How to initialize static variables
Instead of finding a way to get static variables working, I prefer to simply create a getter function. Also helpful if you need arrays belonging to a specific class, and a lot simpler to...
View ArticleAnswer by Emanuel Landeholm for How to initialize static variables
If you have control over class loading, you can do static initializing from there.Example:class MyClass { public static function static_init() { } }in your class loader, do the following:include($path...
View ArticleAnswer by Kornel for How to initialize static variables
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(…);orclass Foo {...
View ArticleAnswer by Alister Bulman for How to initialize static variables
That's too complex to set in the definition. You can set the definition to null though, and then in the constructor, check it, and if it has not been changed - set it:private static $dates =...
View ArticleAnswer by alxp for How to initialize static variables
You can't make function calls in this part of the code. If you make an init() type method that gets executed before any other code does then you will be able to populate the variable then.
View ArticleHow to initialize static variables
I have this code:private static $dates = array('start' => mktime( 0, 0, 0, 7, 30, 2009), // Start date'end' => mktime( 0, 0, 0, 8, 2, 2009), // End date'close' => mktime(23, 59, 59, 7, 20,...
View Article