(PHP) Singleton Database Class - what about static methods?

This is a basic website. Based on the answers here, I do this:

private $db; public function __construct($id = null) { $this->db = Db::getInstance(); //singleton from the Db class 

But if there is a static method, I cannot use an object-specific variable.

Is there anything better than manually specifying a db variable inside a static method?

 public static function someFunction($theID){ $db = Db::getInstance(); 

EDIT: creating a static variable does not solve the problem. Access to undeclared static property . I still have to assign a variable in a static function. The question is, asks if there is a way around this.

My DB class (although this is not important for this discussion):

 class Db { private static $m_pInstance; private function __construct() { ... } public static function getInstance(){ if (!self::$m_pInstance) self::$m_pInstance = new Db(); return self::$m_pInstance; } 

}

+3
source share
2 answers

Yes, you can do $db static:

 static private $db; 

I guess what you need as you access it using the static method. If there is any reason why you do not want this, this should mean that the method probably should not be static .

EDIT:

According to the comments of @zerkms (thanks), you access the static variables using self:: :::

 self::$db = Db::getInstance(); 
+5
source

You described one of the problems that you encounter when mixing static methods inside a class, which can also be far-fetched, setting member variables through instantiation and waiting for access to their values ​​through a call to the static method. The only real way is to set up a single classmate for a single database or pass a database object to a static method.

 // Option 1 class MyClass { private static $db; public function __construct($id = null) { self::$db = Db::getInstance(); //singleton from the Db class } public static function someFunction($theID) { self::$db->query('SELECT * FROM my_table'); } } // Singleton DB for MyClass will be initalized via constructor $myClass = new MyClass(); // This call will have access to DB object set via class specific singleton MyClass::someFunction(4); // Option 2 class MyClass { private $db; public function __construct($id = null) { $this->$db = Db::getInstance(); //singleton from the Db class if (!is_null($id)) { $this->id = $id; } } public function getDb() { return $this->db; } public function getId() { return $this->id; } // Sub-Option 1: If Id ISNT set via object public static function someFunction($object, $theID) { $object->getDb()->query('SELECT * FROM my_table WHERE id = ' . (int) $theID); } // Sub-Option 2: If Id IS set via object public static function someFunction($object) { $object->getDb()->query('SELECT * FROM my_table WHERE id = ' . (int) $object->getId()); } } // Sub-Option 1 call $myClass = new MyClass(); MyClass::someFunction($myClass, 4); // Sub-Option 2 call $myClass = new MyClass(4); MyClass::someFunction($myclass); 
+2
source

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


All Articles