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

Answer by Mambazo for How to initialize static variables

$
0
0

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 static bar()    {        [[ do something with self::getFoo() ]]    }}

But an even better solution is to do away with the static methods and use the Singleton pattern. Then you just do the complicated initialization in the constructor. Or make it a "service" and use DI to inject it into any class that needs it.


Viewing all articles
Browse latest Browse all 11

Trending Articles



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