Mysql PDO Wrapper that throws Unique Constraint Exception The main reasoning behind this PDO wrapper, is that I find myself using unique constraints quite frequently in my designs, and I have if ($ex-&gt;errorInfo[1] == 1062) littered throughout my code, and I thought there has to be a better way.
&lt;?php

class PDOMysql extends \PDO {

    public function __construct($dsn, $username = null, $passwd = null, $options = null)
    {
        parent::__construct($dsn, $username, $passwd, $options);
        $this-&gt;setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $this-&gt;setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_OBJ);
    }

    public function prepareAndExecute($statement, array $params = []): \PDOStatement
    {
        try {
            $stmt = $this-&gt;prepare($statement);
            $stmt-&gt;execute($params);
            return $stmt;
        } catch (\PDOException $ex) {
            if ($ex-&gt;errorInfo[1] == 1062) {
                throw new PDOMysqlUniqueConstraintException($ex);
            } else {
                throw $ex;
            }
        }
    }

}


class PDOMysqlUniqueConstraintException extends \PDOException {

    public function __construct(\PDOException $ex)
    {
        parent::__construct($ex-&gt;getMessage(), $ex-&gt;getCode(), $ex-&gt;getPrevious());
    }

}


// example usage 
$user = 'vps'; 
$pass = 'vps'; 
$db = new PDOMysql(&quot;mysql:host=localhost;dbname=testdb&quot;, $user, $pass);

// create table 
$sql = &quot;create table if not exists `mytable` (col1 varchar(100) null, constraint idx_u unique (col1) )&quot;;
$db-&gt;prepareAndExecute($sql);

// insert first row 

$params = ['col1' =&gt; uniqid('')]; 
$sql = &quot;insert into mytable (col1) values (:col1)&quot;; 
$db-&gt;prepareAndExecute($sql, $params);

// select row 
$sql = &quot;select * from mytable where col1 = :col1&quot;; 
$stmt = $db-&gt;prepareAndExecute($sql, $params); 
var_dump($stmt-&gt;fetchAll());

// insert duplicate row 
try {
    $sql = &quot;insert into mytable (col1) values (:col1)&quot;; 
    $db-&gt;prepareAndExecute($sql, $params);
} catch (\PDOMysqlUniqueConstraintException $ex) {
   die('Unique Constraint Detected');
}
This code is so good that it's the first time I have nothing to say.
Your approach to the problem is absolutely correct and you should keep using it.
There could be only microscopic nuances, such as one mentioned by Sam. Or, the fact that your constructor would override the configuration settings passed in the $options array. Say, if someone would want to use FETCH_ASSOC as a default fetch mode, your class won't let them. I would rather make it this way
$defaults = [
    PDO::ATTR_ERRMODE            =&gt; PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_OBJ,
    PDO::ATTR_EMULATE_PREPARES   =&gt; false, // I would throw in this one too
];
$options = array_merge($defaults,$options); 
parent::__construct($dsn, $username, $passwd, $options);

So this way you would have your preferred defaults that, however, could be overwritten, making your class flexible, without artificial restrictions.