php / php/php-src

PDOException::getCode() return value type is string

未关闭
#9,529 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Status: Requires RFC
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先检查 Zend/zend_exceptions.stub.php 中链接的 Throwable 声明,并在 PHP 8.0.21 上复现 PDOException 示例。跟踪异常代码的表示和检查方式,然后确定拟议的 int|string 返回类型的兼容性要求。当预期行为以及受影响的测试或 API 约束均已记录并验证后,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
php
领域
backend-api-design
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。