note 117632 added to class.pdoexception
| From: | jesse dot chisholm at gmail dot com | Date: | Sun, 12 Jul 2015 14:27:08 +0000 |
| Subject: | note 117632 added to class.pdoexception | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203085@lists.php.net to get a copy of this message | ||
In response to the 15-May-2010 07:45 note from: samuelelliot+php dot net at gmail dot com
Since the most base class Exception takes three arguments, and the previous exception is not the
first, ...
Since inherited classes to not implicitly call the parent constructor, ...
I recommend changing his constructor to this:
public function __construct(string $message = "", int $code = 0, PDOException $e = null)
{
// in case they call: new MyException($somePDOException);
// instead of following the interface.
//
if (is_subclass_of($message, PDOException))
{
$e = $message;
$code = $e->getCode();
$message = $e->getMessage();
}
// Let PDOException do its normal thing
//
parent::__construct($message, $code, $e);
// Now to correct the code number.
//
$state = $this->getMessage();
if(!strstr($state, 'SQLSTATE['))
$state = $this->getCode();
if(strstr($state, 'SQLSTATE['))
{
preg_match('/SQLSTATE\[(\w+)\] \[(\w+)\] (.*)/', $state, $matches);
$this->code = ($matches[1] == 'HT000' ? $matches[2] : $matches[1]);
$this->message = $matches[3];
}
}
----
Server IP: 72.52.91.14
Probable Submitter: 2602:306:bda3:e040:e9bb:c49:1142:63cf
----
Manual Page -- http://php.net/manual/en/class.pdoexception.php
Edit -- https://master.php.net/note/edit/117632
Del: integrated -- https://master.php.net/note/delete/117632/integrated
Del: useless -- https://master.php.net/note/delete/117632/useless
Del: bad code -- https://master.php.net/note/delete/117632/bad+code
Del: spam -- https://master.php.net/note/delete/117632/spam
Del: non-english -- https://master.php.net/note/delete/117632/non-english
Del: in docs -- https://master.php.net/note/delete/117632/in+docs
Del: other reasons-- https://master.php.net/note/delete/117632
Reject -- https://master.php.net/note/reject/117632
Search -- https://master.php.net/manage/user-notes.php