Levi Morrison


PHP RFC: ReflectionType Improvements


For PHP 7.0 the ReflectionType API was introduced in conjunction with return types. Its minimal API was intended to be the base for future improvements; this RFC contains some of those improvements.


This RFC adds two new subtypes for ReflectionType; here are their stubs:

class ReflectionNamedType extends ReflectionType {
    public function getName(): string;
class ReflectionClassType extends ReflectionNamedType {
    public function getClass(): ReflectionClass;

If the engine does not recognize a type it will call the autoloader; if the class is found then a ReflectionClassType will be returned. Presently in all other cases a plain ReflectionNamedType will be returned but this may change in the future. For instance if we had intersection types they would be unnamed and therefore would be different subtype of ReflectionType.

A call to the getName() method of a ReflectionNamedType that represents any of int, ?int or int $foo = null will return int in each case. Note that the __toString method would return int, ?int and ?int respectively. Note that the names of builtin types are normalized, so getName() on builtin types will always be in lower case and thus safe for string comparisons.

Backward Incompatible Changes

There is a change for ReflectionType::__toString() which will now include a ? for nullable types. This change is justified for several reason:

  1. ReflectionType was introduced in 7.0 and therefore adoption is low
  2. The change reflects the intention of the meaning of __toString to be the string representation of the type that would pass syntax checking. Unfortunately this intention wasn't formally documented thus classifying this as a BC break.

PHP Version

This RFC targets PHP 7.Next which is currently version 7.1.

Unaffected PHP Functionality

ReflectionClass is unaffected; this only touches ReflectionType.

Patches and Tests

The patch for this is not difficult; it will be coming soon.


An option needs 2/3 votes to win

Accept ReflectionType Improvements RFC? (38.5% approved)
User Vote
ajf No
bishop Yes
bwoebi Yes
dmitry No
guilhermeblanco Yes
kalle No
levim Yes
mariano No
nikic No
ocramius No
stas No
trowski Yes
zimt No