# RFC: Throwable code's type generalization

- Status: Declined
- Authors: Wes (@WesNetmo on Twitter)
- Date: 2016-12-18
- Version: 0.1
- Original: https://wiki.php.net/rfc/throwable-code-generalization
- Page: https://externals.io/rfc/throwable-code-generalization
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Accept Throwables error code generalization? (closed)

Yes: 13 · No: 11

Voters: aharvey (No), andrey (Yes), ashnazg (Yes), bishop (Yes), bwoebi (Yes), danack (No), dm (Yes), galvao (No), guilhermeblanco (No), hywan (No), krakjoe (Yes), lcobucci (No), leigh (No), marcio (Yes), mariano (Yes), mightyuhu (No), mike (Yes), nikic (Yes), ocramius (No), pollita (Yes), stas (Yes), tpunt (No), yohgaki (Yes), zimt (No)

## Discussions on the mailing list

- [\[RFC\]\[DISCUSSION\] - Throwables error code's type generalization](https://externals.io/message/97434.md) — started on 2016-12-18, 7 messages
- [\[RFC\]\[VOTE\] Throwable error code's type generalization](https://externals.io/message/97532.md) — started on 2017-01-06, 14 messages

---

## 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](https://3v4l.org/BoF4U), 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](http://externals.io/thread/573)
