Throwable code's type generalization View as Markdown

Declined
Authors
Wes (@WesNetmo on Twitter)
Date
2016-12-18
Version
0.1
Source
wiki.php.net/rfc/throwable-code-generalization

Vote

Accept Throwables error code generalization? closed
Yes
13
No
11
24 voters
aharvey No 2017-01-09 01:53
andrey Yes 2017-01-09 14:20
ashnazg Yes 2017-01-08 18:32
bishop Yes 2017-01-13 16:40
bwoebi Yes 2017-01-06 18:08
danack No 2017-01-21 10:38
dm Yes 2017-01-18 09:20
galvao No 2017-01-06 19:24
guilhermeblanco No 2017-01-13 17:59
hywan No 2017-01-10 12:38
krakjoe Yes 2017-01-08 15:37
lcobucci No 2017-01-07 15:02
leigh No 2017-01-17 21:28
marcio Yes 2017-01-19 03:24
mariano Yes 2017-01-18 07:49
mightyuhu No 2017-01-19 09:07
mike Yes 2017-01-10 08:55
nikic Yes 2017-01-15 15:19
ocramius No 2017-01-06 17:34
pollita Yes 2017-01-17 23:01
stas Yes 2017-01-06 18:59
tpunt No 2017-01-15 18:45
yohgaki Yes 2017-01-19 09:51
zimt No 2017-01-25 13:21

Discussions on the mailing list

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:52 UTC

Introduction

Currently Exception::$code, Error::$code and Throwable::getCode() are said to be int only, but this is actually violated by PHP itself, specifically by the PDO extension, whose error codes are strings instead.

As such, code that assumes that the interface always uses int is already potentially broken - because it doesn't special-case PDO - or it does already special-case the type so that PDO's errors don't create issues.

Proposal

As this inconsistency is present, this RFC proposes to generalize the Throwable's code so that it can be of any type - not just int. In practice this is mostly a documentation change because the int type is never enforced, except for Exception and Error constructors.

Throwables' codes are normally not needed, and even if they were, numeric ones aren't comfortable to work with. Allowing any type can open up to new possibilities for this property that is currently almost useless, like Enum/EnumSet-like objects, strings, etc.

Specifically, the following members are proposed to be modified:

  • Throwable::getCode(): mixed;
  • Error::__construct(string $message = “”, mixed $code = 0, Throwable $previous = null);
  • Exception::__construct(string $message = “”, mixed $code = 0, Throwable $previous = null);
  • protected mixed Error::$code;
  • protected mixed Exception::$code;

Backward Incompatible Changes

None.

Proposed PHP Version(s)

7.2

Voting

2/3 majority is required. Voting will end on 27th of Jan.

Vote: Accept Throwables error code generalization? — Yes: 13 · No: 11 (closed)

Patches and Tests

N/A

References

Discussion on externals.io