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

Answer by David Spector for How to initialize static variables

$
0
0

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 classes don't have constructors, just add a manual constructor to initialize any variables requiring nontrivial calculation:

class A   {   static $a; // Initialized by Init()   static function Init()      {      A::$a=nontrivial();      {   }...A::Init();    // Initialize static part of class...$obj=new A(); // Using initialized class as an object

Viewing all articles
Browse latest Browse all 11

Trending Articles



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