PHP Reflection
**************
.[perex]
[api:Nette\Utils\Reflection] is a static class providing useful functions for PHP reflection. Its purpose is to address shortcomings in native reflection classes and unify behavior across different PHP versions.
Installation:
```shell
composer require nette/utils
```
All examples assume the following class alias is defined:
```php
use Nette\Utils\Reflection;
```
areCommentsAvailable(): bool .[method]
--------------------------------------
Checks if reflection has access to PHPdoc comments. Comments might be unavailable due to opcode caching, see for example the [opcache.save-comments|https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.save-comments] directive.
expandClassName(string $name, ReflectionClass $context): string .[method]
-------------------------------------------------------------------------
Expands the class name `$name` to its fully qualified name within the context of the class `$context`, considering its namespace and defined aliases. Essentially, it determines how the PHP parser would interpret `$name` if it were written inside the body of the `$context` class.
```php
namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass(Foo\DemoClass::class);
Reflection::expandClassName('Bar', $context); // 'Bar'
Reflection::expandClassName('Baz', $context); // 'Foo\Baz'
```
getMethodDeclaringMethod(ReflectionMethod $method): ReflectionMethod .[method]
------------------------------------------------------------------------------
Returns the reflection of the method that contains the actual declaration of `$method`. Usually, a method is its own declaration, but the method body might originate from a trait and potentially under a different name.
Since PHP doesn't provide sufficient information to determine the true declaration reliably, Nette uses its own heuristics, which **should be** reliable.
```php
trait DemoTrait
{
function foo()
{
}
}
class DemoClass
{
use DemoTrait {
DemoTrait::foo as foo2;
}
}
$method = new ReflectionMethod('DemoClass::foo2');
Reflection::getMethodDeclaringMethod($method); // ReflectionMethod('DemoTrait::foo')
```
getParameterDefaultValue(ReflectionParameter $param) .[method]
--------------------------------------------------------------
Returns the default value of `$param` in a function or method. If it is a constant, it returns its value. If the parameter does not have a default value or the constant cannot be resolved, it throws `ReflectionException`.
```php
class DemoClass
{
function foo($param = PDO::FETCH_BOTH)
{
}
}
$method = new ReflectionMethod('DemoClass::foo');
Reflection::getParameterDefaultValue($method->getParameters()[0]);
```
getParameterType(ReflectionParameter $param): ?string .[method deprecated]
--------------------------------------------------------------------------
Returns the type of the function or method `$param` parameter and resolves `self` and `parent` to the actual class name. If the parameter has no type, it returns `null`, if it has a union or intersection type it throws `Nette\InvalidStateException`.
```php
class DemoClass
{
function foo(self $param)
{
}
}
$method = new ReflectionMethod('DemoClass::foo');
Reflection::getParameterType($method->getParameters()[0]); // 'DemoClass'
```
This method was created when PHP did not have union and intersection types. It is replaced by the method [Nette\Utils\Type::fromReflection()|type#fromReflection].
getPropertyDeclaringClass(ReflectionProperty $prop): ReflectionClass .[method]
------------------------------------------------------------------------------
Returns the reflection of the class or trait that contains the declaration of the property `$prop`. The property might be declared within a trait.
Since PHP doesn't provide sufficient information to determine the true declaration reliably, Nette uses its own heuristics, which **are not** fully reliable.
```php
trait DemoTrait
{
public $foo;
}
class DemoClass
{
use DemoTrait;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
Reflection::getPropertyDeclaringClass($prop); // ReflectionClass('DemoTrait')
```
getPropertyType(ReflectionProperty $prop): ?string .[method deprecated]
-----------------------------------------------------------------------
Returns the type of property `$prop` and resolves `self` and `parent` to the actual class name. If property has no type, it returns `null`, if it has a union or intersection type it throws `Nette\InvalidStateException`.
```php
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
Reflection::getPropertyType($prop); // 'DemoClass'
```
This method was created when PHP did not have union and intersection types. It is replaced by the method [Nette\Utils\Type::fromReflection()|type#fromReflection].
getReturnType(ReflectionFunctionAbstract $func): ?string .[method deprecated]
-----------------------------------------------------------------------------
Returns the return type of the function or method `$func` and resolves `self`, `static` and `parent` to the actual class name. If the function does not have a return type, it returns `null`, if it has a union or intersection type it throws `Nette\InvalidStateException`.
```php
class DemoClass
{
function foo(): self
{
}
}
$method = new ReflectionMethod('DemoClass::foo');
Reflection::getReturnType($method); // 'DemoClass'
```
This method was created when PHP did not have union and intersection types. It is replaced by the method [Nette\Utils\Type::fromReflection()|type#fromReflection].
isBuiltinType(string $type): bool .[method deprecated]
------------------------------------------------------
Checks if `$type` is a PHP built-in type. Otherwise, it's considered a class name.
```php
Reflection::isBuiltinType('string'); // true
Reflection::isBuiltinType('Foo'); // false
```
toString($reflection): string .[method]
---------------------------------------
Converts a reflection object to a human-readable string.
```php
$func = new ReflectionFunction('func');
echo Reflection::toString($func); // 'func' ('func()' since v3.2.0)
$class = new ReflectionClass('DemoClass');
echo Reflection::toString($class); // 'DemoClass'
$method = new ReflectionMethod('DemoClass', 'foo');
echo Reflection::toString($method); // 'DemoClass::foo' ('DemoClass::foo()' since v3.2.0)
$param = new ReflectionParameter(['DemoClass', 'foo'], 'param');
echo Reflection::toString($param); // '$param in DemoClass::foo()'
$prop = new ReflectionProperty('DemoClass', 'foo');
echo Reflection::toString($prop); // 'DemoClass::$foo'
```
{{maintitle: PHP Reflection Helpers in Nette Utils: Classes and Types}}
{{description: Helpers that fill gaps in native PHP reflection and unify it across versions: expand class names, resolve types, find where a method is declared.}}
PHP Reflection
Nette\Utils\Reflection is a static
class providing useful functions for PHP reflection. Its purpose is to address shortcomings in native reflection classes and unify
behavior across different PHP versions.
Installation:
composer require nette/utils
All examples assume the following class alias is defined:
use Nette\Utils\Reflection;
Checks if reflection has access to PHPdoc comments. Comments might be unavailable due to opcode caching, see for example the opcache.save-comments directive.
expandClassName (string $name,
ReflectionClass $context): string
Expands the class name $name to its fully qualified name within the context of the class $context,
considering its namespace and defined aliases. Essentially, it determines how the PHP parser would interpret $name if
it were written inside the body of the $context class.
namespace Foo;
use Bar;
class DemoClass
{
// new Bar, new Baz
}
$context = new ReflectionClass(Foo\DemoClass::class);
Reflection::expandClassName('Bar', $context); // 'Bar'
Reflection::expandClassName('Baz', $context); // 'Foo\Baz'
getMethodDeclaringMethod (ReflectionMethod $method): ReflectionMethod
Returns the reflection of the method that contains the actual declaration of $method. Usually, a method is its own
declaration, but the method body might originate from a trait and potentially under a different name.
Since PHP doesn't provide sufficient information to determine the true declaration reliably, Nette uses its own heuristics,
which should be reliable.
trait DemoTrait
{
function foo()
{
}
}
class DemoClass
{
use DemoTrait {
DemoTrait::foo as foo2;
}
}
$method = new ReflectionMethod('DemoClass::foo2');
Reflection::getMethodDeclaringMethod($method); // ReflectionMethod('DemoTrait::foo')
getParameterDefaultValue (ReflectionParameter $param)
Returns the default value of $param in a function or method. If it is a constant, it returns its value. If the
parameter does not have a default value or the constant cannot be resolved, it throws ReflectionException.
class DemoClass
{
function foo($param = PDO::FETCH_BOTH)
{
}
}
$method = new ReflectionMethod('DemoClass::foo');
Reflection::getParameterDefaultValue($method->getParameters()[0]);
getParameterType (ReflectionParameter $param): ?string
Returns the type of the function or method $param parameter and resolves self and parent
to the actual class name. If the parameter has no type, it returns null, if it has a union or intersection type it
throws Nette\InvalidStateException.
class DemoClass
{
function foo(self $param)
{
}
}
$method = new ReflectionMethod('DemoClass::foo');
Reflection::getParameterType($method->getParameters()[0]); // 'DemoClass'
This method was created when PHP did not have union and intersection types. It is replaced by the method Nette\Utils\Type::fromReflection().
getPropertyDeclaringClass (ReflectionProperty $prop): ReflectionClass
Returns the reflection of the class or trait that contains the declaration of the property $prop. The property
might be declared within a trait.
Since PHP doesn't provide sufficient information to determine the true declaration reliably, Nette uses its own heuristics,
which are not fully reliable.
trait DemoTrait
{
public $foo;
}
class DemoClass
{
use DemoTrait;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
Reflection::getPropertyDeclaringClass($prop); // ReflectionClass('DemoTrait')
getPropertyType (ReflectionProperty $prop): ?string
Returns the type of property $prop and resolves self and parent to the actual class
name. If property has no type, it returns null, if it has a union or intersection type it throws
Nette\InvalidStateException.
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
Reflection::getPropertyType($prop); // 'DemoClass'
This method was created when PHP did not have union and intersection types. It is replaced by the method Nette\Utils\Type::fromReflection().
getReturnType (ReflectionFunctionAbstract $func): ?string
Returns the return type of the function or method $func and resolves self, static and
parent to the actual class name. If the function does not have a return type, it returns null, if it has
a union or intersection type it throws Nette\InvalidStateException.
class DemoClass
{
function foo(): self
{
}
}
$method = new ReflectionMethod('DemoClass::foo');
Reflection::getReturnType($method); // 'DemoClass'
This method was created when PHP did not have union and intersection types. It is replaced by the method Nette\Utils\Type::fromReflection().
isBuiltinType (string
$type): bool
Checks if $type is a PHP built-in type. Otherwise, it's considered a class name.
Reflection::isBuiltinType('string'); // true
Reflection::isBuiltinType('Foo'); // false
toString ($reflection):
string
Converts a reflection object to a human-readable string.
$func = new ReflectionFunction('func');
echo Reflection::toString($func); // 'func' ('func()' since v3.2.0)
$class = new ReflectionClass('DemoClass');
echo Reflection::toString($class); // 'DemoClass'
$method = new ReflectionMethod('DemoClass', 'foo');
echo Reflection::toString($method); // 'DemoClass::foo' ('DemoClass::foo()' since v3.2.0)
$param = new ReflectionParameter(['DemoClass', 'foo'], 'param');
echo Reflection::toString($param); // '$param in DemoClass::foo()'
$prop = new ReflectionProperty('DemoClass', 'foo');
echo Reflection::toString($prop); // 'DemoClass::$foo'