Quantcast
Channel: How to initialize static variables - Stack Overflow
Browsing latest articles
Browse All 11 View Live

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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

How 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
Browsing latest articles
Browse All 11 View Live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>