In PHP, the constructor does not return.
Thus, your get method returns one , the first time it is called, and then the mysqli object. Probably not what you want.
if( self::$_db == NULL ) { return new self();
If you want to return a mysqli object, you do not need a singlet, since there is no need to create an instance of an object that only returns an instance of another object here.
In this case, the registry template will be better.
If you need to provide methods (a wrapper for your database object), then create a real singleton.
EDIT
I checked your updated code. Now you always return a mysqli instance. But you do not need to instantiate your own object. It is completely useless ...
If you really want to go with your template, as golden said, in your static instance, check if self::db NULL . If so, create a mysqli instance and assign it self::db . Then returns it.
public static getDatabaseInstance() { if( self::$_db == NULL ) { self::$_db = new mysqli( ... ); } return self::$_db; }
Also, set the private constructor to prevent users from creating useless instances of your class. Or is it better to make it public and throw an exception:
public function __construct() { throw new Exception( 'This class is not supposed to be instantiated' ); }
source share