Trying to make singletone PDO with a namespace. Here is the Db connection class
namespace liw\components; class Db { private static $_db = null; private function __construct() {} private function __clone() {} private function __wakeup() {} public static function getInstance() { if (self::$_db === null) { $dsn = "mysql:host=localhost;dbname=table"; self::$_db = new PDO($dsn, 'root', ''); } return self::$_db; } } Attempts to access class Db in the model result in an error
Class 'liw \ components \ PDO' not found
namespace liw\models; class Test { private $_db = null; public function __construct() { $this->_db = \liw\components\Db::getInstance(); } public function getList() { $list= array(); $result = $this->_db->query("SELECT * FROM table"); $i = 0; while ($row = $result->fetch()) { $list[$i]['id'] = $row['id']; $i++; } return $list; } } I understand that there is an attempt to create a PDO class that does not have a namespace. How to fix it?
spl_autoload_registermade? In PHP, there is no automatic pickup of classes, as in Java)) - Alexey Shimansky