This is an old revision of the document!
PHP RFC: Your Title Here
- Version: 1.0
- Date: 2016-06-10
- Author: Levi Morrison levim@php.net
- Status: Draft
- First Published at: http://wiki.php.net/rfc/ReflectionTypeImprovements
Introduction
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 are those improvements.
Proposal
This RFC adds several new subtypes for ReflectionType
; here is their hierarchy:
- ReflectionType (already exists)
- ReflectionSingularType
- ReflectionIntType
- ReflectionFloatType
- ReflectionBoolType
- ReflectionStringType
- ReflectionArrayType
- ReflectionCallableType
- ReflectionVoidType
- ReflectionClassType
ReflectionSingularType
has a new method named getName()
which returns the string name of the type it represents. Here is a stub the represents its API:
class ReflectionSingularType extends ReflectionType { public function getName(): string; }
A call to the getName()
method of a SingularType
that represents any of int
, ?int
or int $foo = null
will return int
in each case.
ReflectionClassType
has an additional method getClass()
that obtains the ReflectionClass
of the type it represents. Here is its stub:
class ReflectionClassType extends ReflectionSingularType { public function getClass(): ReflectionClass; }
The engine will return a subtype of ReflectionSingularType
if possible. For builtin types the engine will return ReflectionIntType
, ReflectionFloatType
, etc as appropriate. If the engine does not recognize a type it will call the autoloader; if the class is found then a ReflectionClassType
will be returned and otherwise a plain ReflectionSingularType
will be returned.
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:
ReflectionType
was introduced in 7.0 and therefore adoption is low- 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
.
Voting
It is unclear if voting requires 50%+1 or 2/3. On one hand this is not a language change but on the other this does contain a BC break. Rather than debating it one way or the other I've chosen to require 2/3.
The vote will be a simple yes or no for these changes.
Patches and Tests
The patch for this is not difficult; it will be coming soon.