PDOException::getCode() return value type is string
Open
Nobody has claimed this yet.
Bug
Status: Requires RFC
- Dominant language
- C
- Stars
- 40.4k
- Forks
- 8.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 96
Description
Description
The following code:
<?php
declare(strict_types=1);
try
{
$pdo = new PDO('mysql:host=127.0.0.1', 'root', 'root');
$pdo->query('select x');
}
catch (\Throwable $e)
{
var_dump($e->getCode());
throw new \Exception($e->getMessage(), $e->getCode(), $e);
}
Resulted in this output:
string(5) "42S22"
PHP Fatal error: Uncaught TypeError: Exception::__construct(): Argument #2 ($code) must be of type int, string given in /mnt/d/projects/imi/imi-2.1/test.php:13
Stack trace:
#0 /mnt/d/projects/imi/imi-2.1/test.php(13): Exception->__construct()
#1 {main}
thrown in /mnt/d/projects/imi/imi-2.1/test.php on line 13
But I expected this output instead:
string(5) "42S22"
PHP Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'x' in 'field list' in /mnt/d/projects/imi/imi-2.1/test.php:8
Stack trace:
#0 /mnt/d/projects/imi/imi-2.1/test.php(8): PDO->query()
#1 {main}
Next Exception: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'x' in 'field list' in /mnt/d/projects/imi/imi-2.1/test.php:13
Stack trace:
#0 {main}
thrown in /mnt/d/projects/imi/imi-2.1/test.php on line 13
Is it possible to change the return value of Throwable::getCode() to int|string?
PHP Version
8.0.21
Operating System
Ubuntu 20.04
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the linked Throwable declaration in Zend/zend_exceptions.stub.php and reproduce the PDOException example on PHP 8.0.21. Trace how exception codes are represented and checked, then determine the compatibility requirements for the proposed int|string return type. Done means the intended behavior and affected tests or API constraints are documented and validated.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100