Skip to content

PDOException::getCode() return value type is string #9529

Open
@Yurunsoft

Description

@Yurunsoft

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?

public function getCode(); // TODO add proper type (i.e. int|string)

PHP Version

8.0.21

Operating System

Ubuntu 20.04

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions