расширение класса PDO

Ниже приведен класс соединений db, с которым я вышел до сих пор, но я собираюсь улучшить его, расширив сам класс PDO,

<?php class database { protected $connection = null; #make a connection public function __construct($hostname,$dbname,$username,$password) { try { # MySQL with PDO_MYSQL $this->connection = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { $this->connection = null; die($e->getMessage()); } } #get the number of rows in a result public function num_rows($query) { # create a prepared statement $stmt = $this->connection->prepare($query); if($stmt) { # execute query $stmt->execute(); return $stmt->rowCount(); } else { return self::get_error(); } } #display error public function get_error() { $this->connection->errorInfo(); } # closes the database connection when object is destroyed. public function __destruct() { $this->connection = null; } } ?> 

расширенный класс,

 class database extends PDO { #make a connection public function __construct($hostname,$dbname,$username,$password) { parent::__construct($hostname,$dbname,$username,$password); try { $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { die($e->getMessage()); } } #get the number of rows in a result public function num_rows($query) { # create a prepared statement $stmt = parent::prepare($query); if($stmt) { # execute query $stmt->execute(); return $stmt->rowCount(); } else { return self::get_error(); } } #display error public function get_error() { $this->connection->errorInfo(); } # closes the database connection when object is destroyed. public function __destruct() { $this->connection = null; } } 

Вот как я создаю класс,

 # the host used to access DB define('DB_HOST', 'localhost'); # the username used to access DB define('DB_USER', 'root'); # the password for the username define('DB_PASS', 'xxx'); # the name of your databse define('DB_NAME', 'db_2011'); include 'class_database.php'; $connection = new database(DB_HOST,DB_NAME,DB_USER,DB_PASS); $sql = " SELECT * FROM root_contacts_cfm ORDER BY cnt_id DESC "; $connection->num_rows($sql); 

Но у меня есть ошибки, когда я называю этот расширенный класс pdo,

Предупреждение: PDO :: __ construct () ожидает, что параметр 4 будет массивом, строка задана в C: \ wamp \ www \ xx \ class_database.php в строке xx

    Неустранимая ошибка: вызовите функцию-член setAttribute () для не-объекта в C: \ wamp \ www \ xx \ class_database.php в строке xx

    Я провел несколько исследований в Интернете, я нашел эту основную структуру расширения pdo, но я не понимаю этого …

     class myPDO extends PDO { public function __construct($dsn, $username=null, $password=null, $driver_options=null) { parent::__construct($dsn, $username, $password, $driver_options); } public function query($query) { $result = parent::query($query); // do other stuff you want to do here, then... return($result); } } 

    Что такое переменная $ds n? Как передать переменную $hostname в расширенный класс pdo?

    Другие вопросы: Как я могу создать метод для отображения ошибки в расширенном классе pdo? Как закрыть соединение в расширенном классе pdo?

    Это так сложно переместиться из mysqli в pdo!

    Благодарю.

    Related of "расширение класса PDO"

    $ dsn – это имя источника данных. Он обрабатывает ваше имя хоста для вас. Вы используете его следующим образом:

     $dsn = 'mysql:dbname=YOUR_DB_NAME;host=YOUR_HOSTNAME' 

    С помощью строки $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Вы устанавливали исключения, возникающие при возникновении ошибок (что мне нравится), поэтому в расширенном классе вы можете обрабатывать ошибки в обработчиках исключений. Если у вас был метод getAssoc в расширенном классе PDO, он выглядел бы так:

     /// Get an associative array of results for the sql. public function getAssoc($sql, $params=array()) { try { $stmt = $this->prepare($sql); $params = is_array($params) ? $params : array($params); $stmt->execute($params); return $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $e) { // Echo the error or Re-throw it to catch it higher up where you have more // information on where it occurred in your program. // eg echo 'Error: ' . $e->getMessage(); throw new Exception( __METHOD__ . 'Exception Raised for sql: ' . var_export($sql, true) . ' Params: ' . var_export($params, true) . ' Error_Info: ' . var_export($this->errorInfo(), true), 0, $e); } } 

    Я хотел бы сосредоточиться на том, что должен делать класс, а не пытаться переписать PDO. У меня была такая же идея, потому что я бы упростил и передал поток кода, но это не так. В конечном итоге вы тратите больше времени на то, как косвенно взаимодействовать с PDO.