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

Answer by espaciomore for How to initialize static variables

$
0
0

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 function db(){ if( !isset(static::$db) ){  static::$db= new \Helpers\MySQL( array("hostname"=> "localhost","username"=> "root","password"=> "password","database"=> "db_name"    )  ); } return static::$db;}

then you can do static::db(); or self::db(); from anywhere.


Viewing all articles
Browse latest Browse all 11

Trending Articles



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