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:
All examples assume the following class alias is defined:
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
.
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.
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.
isBuiltinType(string $type): bool
Determines if $type
is PHP built-in type. Otherwise, it is the class name.
Use Nette\Utils\Validator::isBuiltinType().
toString($reflection): string
Converts a reflection to a human readable string.