Simpler Singleton

All singleton templates I've seen use an object reference to determine if the object was created. However, if I use singleton to guarantee only one db connection, why not use the db communication resource link for this? Here is the code I'm using. (PS: it works great). I use commentary to be able to easily search for my classes.

/*one*/ class one { public static $db; private function __construct() { self::$db=new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE); } public static function get() { if(self::$db==NULL) { new self(); } return self::$db; } } 
+6
source share
5 answers

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(); // Here you return an object of class one } else { return self::$_db; // Here you return an object of type mysqli } 

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' ); } 
+5
source

Determine that it works great :)

Try:

  • Compare hash objects of objects returned from both methods (it matters when using cloning objects)
  • connect to the database using different credentials (for example, in unit tests)
  • disconnect from the database and reconnect
  • reset an instance of an object (now creating 1000 objects using new fills the memory up)
  • tell another developer to create, for example, this class, he will certainly find a way one::getInstance() . How does he know the behavior of this class?

Singlotons are a global state. It looks like you have a global state + some kind of mess.

+3
source

Assuming I am parsing your question correctly, you are asking if it is correct to use the null $db value to make sure that you are only creating an instance; which is the right way to do something, and actually this is what I would recommend. PHP null explicitly intended to represent a variable with "no value" - ideal for the uninitialized state of a singleton template.

Typically, these things will simply be invoked by an intuitive name, for example. SomeAppDbConn .

+1
source
 class one { private static $_selfInstance; public $db; private function __construct() { } public function getDb() { if($this->db == null) $this->db=new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE); return $this->db; } public static function getInstance() { if( !(self::$_selfInstance instanceof self) ) { self::$_selfInstance= new self(); } return self::$_selfInstance; } } 

Access

 $db = one::getInstance()->getDb(); 
0
source

As Macmade already pointed out, the constructor method returns nothing but an instance of the class. Assuming you always need the same mysqli instance, here is how I do it.

 class DBInstance { protected static $db; private function __construct() { // intentionally empty } public static function get() { if(self::$db === NULL) { self::$db=new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE); } return self::$db; } } 

This, of course, is a singleton template with an added bonus separating the instance from the created class.

0
source

Source: https://habr.com/ru/post/897026/


All Articles