How can this be done without crutches and tambourines?
Desirable this type:
try { $db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); } catch (PDOException $e) { die('PDO->ERROR::'.$e->getMessage()); } I decided to try Singleton:
class DB extends PDO { private $db; public static $Instance = NULL; public static function Instance() { if (self::$Instance == NULL) { self::$Instance = new self(); } return self::$Instance; } public function __construct() { $this->db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); } public function prepare($sql, $values) { $sth = $this->db->prepare($sql); $sth->execute($values); } } But again, the mistakes ...
Fatal error: Call to a member function fetch () on a non-object in.
How to avoid them without making a wrapper?
__constructis protected so that the "outside" is not done$db = new DB;- copist