php / php/doc-en

PDOException property $code of type string not int

Open
#3,002 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Extension: pdo
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

I've stumbled across it a few times now that when catching a \PDOException and dumping the code from the exception via getCode() method that the return value seems to be of type string.

Example:

// some syntax error in SQL Query
catch(\PDOException $e) {
    var_dump($e->getCode()); //Output: string(5) "42000"
}

Documentation mentions return type int (see https://www.php.net/manual/en/class.pdoexception.php):

class PDOException extends RuntimeException {
    /* Properties */
    // ...
    /* Inherited properties */
    // ...
    protected int $code;

// ...

As per zend_exceptions.stub.php the property $code of the Exception class is marked with TODO (see https://github.com/php/php-src/blob/7684a3d138f793630e2ac640ac7d3acc4a79467b/Zend/zend_exceptions.stub.php#L37 ):

protected $code = 0;  // TODO add proper type (i.e. int|string) 

Further within zend_exceptions.stub.php no return type is declared for the getCode() method (see https://github.com/php/php-src/blob/7684a3d138f793630e2ac640ac7d3acc4a79467b/Zend/zend_exceptions.stub.php#L53 ):

   /**
     * @return int
     * @implementation-alias Exception::getCode
     */
    final public function getCode() {} 

Additionally I'd like to quote Core_c.php in getCode() of JetBrains PhpStorm which also mentions the same:

Gets the Exception code
Returns:
int|mixed the exception code as integer in Exception but possibly as other type in Exception descendants (for example as string in PDOException).
Links:
https://php.net/manual/en/exception.getcode.php

Contributor guide

No contributing guide indexed for this repository

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 with the PDOException documentation linked in the issue and compare its documented code property and getCode() return type with Zend/zend_exceptions.stub.php. Confirm the behavior shown for PDOException, then update the relevant documentation to describe the possible string value and verify the rendered API reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.