1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <?php
class BDDException extends /*PDO*/Exception {}
class MyPDO extends PDO {
public function __construct() {
try {
call_user_func_array(array(get_parent_class(), __FUNCTION__), func_get_args());
} catch (PDOException $e) {
throw new BDDException($e->getMessage(), $e->getCode()/*, $e*/);
}
parent::setAttribute(self::ATTR_ERRMODE, self::ERRMODE_EXCEPTION);
}
public function setAttribute($attribute, $value) {
if ($attribute == self::ATTR_ERRMODE) {
throw new Exception("Modification interdite de l'attribut ATTR_ERRMODE");
}
return call_user_func_array(array(get_parent_class(), __FUNCTION__), func_get_args());
}
public function query($statement) {
try {
return call_user_func_array(array(get_parent_class(), __FUNCTION__), func_get_args());
} catch (PDOException $e) {
throw new BDDException($e->getMessage(), $e->getCode()/*, $e*/);
}
}
}
try {
$dbh = new MyPDO(DSN, LOGIN, MOT_DE_PASSE);
$dbh->query('une requête invalide');
} catch (/*BDD*/Exception $e) {
die(sprintf("%s dans %s à la ligne %d : %s", get_class($e), $e->getFile(), $e->getLine(), $e->getMessage()));
} |
Partager