Scalar Type Declarations
The ReflectionParameter
class represents a function
or method parameter. The hasType()
and
getType()
methods have been added to support scalar
type declarations. Consider the interface shown below:
interface I
{
public function m1(string $a): int;
public function m2(C $c): C;
public function m3($p);
}
Using the hasType()
method we can query whether a
parameter has a type declaration:
$m1 = new ReflectionMethod(I::class, 'm1');
var_dump($m1->getParameters()[0]->hasType());
$m3 = new ReflectionMethod(I::class, 'm3');
var_dump($m3->getParameters()[0]->hasType());
Executing the code shown above will print the output shown below:
bool(true)
bool(false)
Using the getType()
method we can query the type of
a parameter. The result is an object of the new
ReflectionType
class:
$m1 = new ReflectionMethod(I::class, 'm1');
var_dump((string) $m1->getParameters()[0]->getType());
$m2 = new ReflectionMethod(I::class, 'm2');
var_dump((string) $m2->getParameters()[0]->getType());
Executing the code shown above will print the output shown below:
string(6) "string"
string(1) "C"
The methods getClass()
, isArray()
, and
isCallable()
of the ReflectionParameter
class that already existed in PHP 5 continue to work in PHP 7.
As of PHP 7.1, the new
ReflectionNamedType::getName()
method should be used to
retrieve the name of a type instead of simply casting the object to
a string:
function f(int $x): void
{
}
$p = new ReflectionParameter('f', 'x');
var_dump($p->getType()->getName());
Executing the code shown above will print the output shown below:
string(3) "int"
The ReflectionType::__toString()
method was
deprecated in PHP 7.1. This was due to the introduction of nullable
types in that version of PHP. The alternative would have been to
change the implementation of
ReflectionType::__toString()
to return
?int
instead of int
, for instance. This
would have broken code that relied on
ReflectionType::__toString()
to only return the name of
the type without the optional ?
prefix for nullability.
The broken – because it was not updated for nullable types –
implementation of ReflectionType::__toString()
was kept
for backwards compatibility reasons.
The deprecation of ReflectionType::__toString()
was
only documented in the PHP manual. The implementation of this method
was not changed to trigger an E_DEPRECATED
notice. This
was because PHP 7.1 was not able to handle exceptions raised in the
context of __toString()
methods. The use of a custom
error handler that converts E_DEPRECATED
notices into
exceptions would have led to a situation where an exception would
have been raised in the context of a __toString()
method.
As of PHP 7.4, __toString()
methods are allowed to
raise exceptions. The implementation of
ReflectionType::__toString()
has been changed so that
using this method now triggers an E_DEPRECATED
notice.
The implementation of ReflectionType::__toString()
has been updated to correctly handle nullable types in PHP 8 and the
method is no longer deprecated.
function f(?int $x): void
{
}
$parameter = new ReflectionParameter('f', 'x');
$parameterType = $parameter->getType();
var_dump($parameterType->__toString());
Executing the code shown above with PHP 7.4 will print the output shown below:
Deprecated: Function ReflectionType::__toString() is deprecated in ...
string(3) "int"
Executing the same code PHP 8 will print the output shown below:
string(3) "?int"
The ReflectionParameter::allowsNull()
has been added
in PHP 7.1 to check whether a parameter is nullable:
function f(?int $x): void
{
}
$p = new ReflectionParameter('f', 'x');
var_dump($p->allowsNull());
Executing the code shown above will print the output shown below:
bool(true)
Keep in mind that ReflectionParameter::allowsNull()
also returns true
when the parameter does not have a
type declaration at all or when it is variadic. In case you need to
check that a parameter has a type declaration and is nullable (but
not variadic) you should use the following:
if ($parameter->hasType() &&
$parameter->allowsNull() &&
!$parameter->isVariadic()) {
// ...
}
The hasReturnType()
and getReturnType()
methods have been added to the ReflectionFunction
and
ReflectionMethod
classes to support return type
declarations. getReturnType()
returns an object of the
new ReflectionType
class:
$m1 = new ReflectionMethod(I::class, 'm1');
var_dump($m1->hasReturnType());
var_dump($m1->getReturnType()->isBuiltIn());
var_dump((string) $m1->getReturnType());
$m2 = new ReflectionMethod(I::class, 'm2');
var_dump($m2->hasReturnType());
var_dump($m2->getReturnType()->isBuiltIn());
var_dump((string) $m2->getReturnType());
$m3 = new ReflectionMethod(I::class, 'm3');
var_dump($m3->hasReturnType());
Executing the code shown above will print the output shown below:
bool(true)
bool(true)
string(3) "int"
bool(true)
bool(false)
string(1) "C"
bool(false)