Nette Documentation Preview

syntax
PHP Reflection
**************

.[perex]
[api:Nette\Utils\Reflection] is a static class with useful functions for PHP reflection. Its purpose is to fix flaws in native classes and to unify behavior across different versions of PHP.


Installation:

```shell
composer require nette/utils
```

All examples assume the following class alias is defined:

```php
use Nette\Utils\Reflection;
```


areCommentsAvailable(): bool .[method]
--------------------------------------

Finds out if reflection has access to PHPdoc comments. Comments may not be available due to the opcode cache, see for example the directive [opcache.save-comments |https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.save-comments].


expandClassName(string $name, ReflectionClass $context): string .[method]
-------------------------------------------------------------------------

Expands the `$name` of the class to full name in the context of the `$context`, ie in the context of its namespace and defined aliases. Thus, it returns how the PHP parser would understand `$name` if it were written in the body of the `$context`.

```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 a reflection of a method that contains a declaration of `$method`. Usually, each method is its own declaration, but the body of the method can also be in the trait and under a different name.

Because PHP does not provide enough information to determine the actual declaration, 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')
```


getPropertyDeclaringClass(ReflectionProperty $prop): ReflectionClass .[method]
------------------------------------------------------------------------------

Returns a reflection of a class or trait that contains a declaration of property `$prop`. Property can also be declared in the trait.

Because PHP does not provide enough information to determine the actual declaration, Nette uses its own heuristics, which **are not** reliable.

```php
trait DemoTrait
{
	public $foo;
}


class DemoClass
{
	use DemoTrait;
}

$prop = new ReflectionProperty(DemoClass::class, 'foo');
Reflection::getPropertyDeclaringClass($prop); // ReflectionClass('DemoTrait')
```


isBuiltinType(string $type): bool .[method deprecated]
------------------------------------------------------

Determines if `$type` is PHP built-in type. Otherwise, it is the class name.

```php
Reflection::isBuiltinType('string'); // true
Reflection::isBuiltinType('Foo');    // false
```

.[note]
Use [Nette\Utils\Validator::isBuiltinType() |validators#isBuiltinType].


toString($reflection): string .[method]
---------------------------------------

Converts a reflection to a human readable string.

```php
$func = new ReflectionFunction('func');
echo Reflection::toString($func); // 'func()'

$class = new ReflectionClass('DemoClass');
echo Reflection::toString($class); // 'DemoClass'

$method = new ReflectionMethod('DemoClass', 'foo');
echo Reflection::toString($method); // 'DemoClass::foo()'

$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'
```

PHP Reflection

Nette\Utils\Reflection is a static class with useful functions for PHP reflection. Its purpose is to fix flaws in native classes and to unify behavior across different versions of PHP.

Installation:

composer require nette/utils

All examples assume the following class alias is defined:

use Nette\Utils\Reflection;

areCommentsAvailable(): bool

Finds out if reflection has access to PHPdoc comments. Comments may not be available due to the opcode cache, see for example the directive opcache.save-comments.

expandClassName(string $name, ReflectionClass $context)string

Expands the $name of the class to full name in the context of the $context, ie in the context of its namespace and defined aliases. Thus, it returns how the PHP parser would understand $name if it were written in the body of the $context.

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 a reflection of a method that contains a declaration of $method. Usually, each method is its own declaration, but the body of the method can also be in the trait and under a different name.

Because PHP does not provide enough information to determine the actual declaration, 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')

getPropertyDeclaringClass(ReflectionProperty $prop): ReflectionClass

Returns a reflection of a class or trait that contains a declaration of property $prop. Property can also be declared in the trait.

Because PHP does not provide enough information to determine the actual declaration, Nette uses its own heuristics, which are not reliable.

trait DemoTrait
{
	public $foo;
}


class DemoClass
{
	use DemoTrait;
}

$prop = new ReflectionProperty(DemoClass::class, 'foo');
Reflection::getPropertyDeclaringClass($prop); // ReflectionClass('DemoTrait')

isBuiltinType(string $type)bool

Determines if $type is PHP built-in type. Otherwise, it is the class name.

Reflection::isBuiltinType('string'); // true
Reflection::isBuiltinType('Foo');    // false

Use Nette\Utils\Validator::isBuiltinType().

toString($reflection): string

Converts a reflection to a human readable string.

$func = new ReflectionFunction('func');
echo Reflection::toString($func); // 'func()'

$class = new ReflectionClass('DemoClass');
echo Reflection::toString($class); // 'DemoClass'

$method = new ReflectionMethod('DemoClass', 'foo');
echo Reflection::toString($method); // 'DemoClass::foo()'

$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'