Bug #51742 [Com]: A non well formed numeric value encountered
| From: | hanskrentel at yahoo dot de | Date: | Tue, 29 Apr 2014 08:07:00 +0000 |
| Subject: | Bug #51742 [Com]: A non well formed numeric value encountered | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-185493@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=51742&edit=1
ID: 51742
Comment by: hanskrentel at yahoo dot de
Reported by: mike at mikegerwitz dot com
Summary: A non well formed numeric value encountered
Status: Open
Type: Bug
Package: PDO Core
Operating System: All
PHP Version: 5.3.2
Block user comment: N
Private report: N
New Comment:
Here is some working example code, Excpetion::getCode() is final, however Exception::$code
protected.
/**
* Class PDOStatementException
*/
class PDOStatementException extends PDOException
{
/**
* @var string
*/
private $statement;
/**
* @var string
*/
protected $code;
public static function createFrom(PDOException $PDOException, $statement) {
if (!is_string($statement)) {
throw new InvalidArgumentException(
sprintf('Statement must be stirng, %s given', gettype($statement))
);
}
$message = $PDOException->getMessage() . " in statement '" . $statement .
"'";
$sqlstate = $PDOException->getCode();
if (!is_string($sqlstate)) {
throw new LogicException(
sprintf('Expected type of PDOException::getCode() to be string, %s given',
gettype($sqlstate))
);
}
$exception = new PDOStatementException($message, 0, $PDOException);
$exception->code = $sqlstate;
$exception->statement = $statement;
return $exception;
}
/**
* @return string
*/
public function getStatement()
{
return $this->statement;
}
}
Previous Comments:
------------------------------------------------------------------------
[2014-04-29 08:02:10] hanskrentel at yahoo dot de
PDOException violates the interface here to be able to use the SQLSTATE as code which is not
necessarily an integer, but merely a string which might contain a non-numeric value.
Take note that this does not strictly violate the interface, as this has been solved by documenting
in the interface that Exception::;getCode() might return a string:
http://www.php.net/manual/en/exception.getcode.php
So if you want to deal with this, you need to extend from Exception, override the constructor, save
code as a private member and add the getter for it.
------------------------------------------------------------------------
[2011-04-25 18:35:14] bandy dot chris at gmail dot com
Related to bug 39615.
------------------------------------------------------------------------
[2010-09-17 13:46:13] mpartio at gmail dot com
I agree to Mike: PHP should allow non-numeric exception codes. I think this should be pretty easy to
fix?
------------------------------------------------------------------------
[2010-05-04 21:29:57] mike at mikegerwitz dot com
Description:
------------
PDOException extends RuntimeException which in turn extends Exception. The
Exception class has the following constructor:
public __construct ([ string $message = "" [, int $code = 0 [, Exception
$previous = NULL ]]] )
The $code parameter is to be of type integer. However, when using pdo_odbc or
pdo_dblib, the error code returned by the server may be a string, rather than an
integer. So, for example, calling $e->getCode() may yield a value of "12X34".
This is inconsistent with the method definition as well:
final public int getCode ( void )
PHP's own internal library should not produce outcomes that are in conflict with
PHP's definitions.
Test script:
---------------
try
{
$pdo->query( 'BAD QUERY' );
}
catch ( PDOException $e )
{
throw new Exception( $e->getMessage(), $e->getCode() );
}
Expected result:
----------------
// Just a thrown exception, nothing important
Actual result:
--------------
PHP Notice: A non well formed numeric value encountered in [...] on line 7
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=51742&edit=1