php / php/php-src

PDOException::getCode() return value type is string

Open
#9,529 1 comment 0 reactions 0 assignees View on GitHub

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?

https://github.com/php/php-src/blob/eb22f1a31ad02699c718fe15ce648acd401900e4/Zend/zend_exceptions.stub.php#L12

PHP Version

8.0.21

Operating System

Ubuntu 20.04

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.