A performance-improved, and convenient way to deal with PHP Reflection classes.
This library provides a performance-improved, and convenient way to deal with PHP Reflection classes.
Performance: Reflection is slow if you instantiate it continuously. If you rely on reflection heavily to inspect the same type of classes in one request over and over again, then caching of the created Reflection classes helps a lot.
Convenience: Instead of dealing with the object graph of Reflection classes, a facade offers convenient methods to
Use Composer to install this library:
$ composer require scaleupstack/reflection
The public API of this package is available via the ScaleUpStack\Reflection\Reflection
class. All methods of the Reflection
class are static. (In fact, it is not possible to instantiate it.)
Namespace
use ScaleUpStack\Reflection\Reflection;
Methods to retrieve PHP Reflection objects via class name or object:
Reflection::classByName(string $className) : \ReflectionClass
Reflection::classByObject(object $object) : \ReflectionClass
Reflection::propertyOfClass(string $className, string $propertyName) : \ReflectionProperty
Reflection::propertyOfObject(object $object, string $propertyName) : \ReflectionProperty
Reflection::allPropertiesOfClass(string $className) : \ReflectionProperty[]
Reflection::allPropertiesOfObject(object $object) : \ReflectionProperty[]
Reflection::methodOfClass(string $className, string $methodName) : \ReflectionMethod
Reflection::methodOfObject(object $object, string $methodName) : \ReflectionMethod
Reflection::allMethodsOfClass(string $className) : \ReflectionMethod[]
Reflection::allMethodsOfObject(object $object) : \ReflectionMethod[]
Please note that classByObject()
returns a \ReflectionClass
and not a \ReflectionObject
.
Methods to access static and non-static properties:
Reflection::getPropertyValue(object $object, string $propertyName) : mixed
Reflection::getStaticPropertyValue(string $className, string $propertyName) : mixed
Reflection::setPropertyValue(object $object, string $propertyName, $value) : void
Reflection::setStaticPropertyValue(string $className, string $propertyName, $value) : void
Methods to invoke static and non-static methods:
Reflection::invokeMethod(object $object, string $methodName, array $arguments) : mixed
Reflection::invokeStaticMethod(string $className, string $methodName, array $arguments) : mixed
This library is stable.
Handling of some Reflection features that could (perhaps?) benefit from caching (e.g. getting the parent class) are not implemented yet. But I do not strive for completeness. If you are missing some features, just create a pull request or ask for it, explaining your context/needs.
Thanks that you want to contribute to ScaleUpStack/Reflection.
Report any bugs or issues on the issue tracker.
Get the source code from the Git repository.
Please check LICENSE.md in the root dir of this package.
ScaleUpVentures Gmbh, Germany
Thomas Nunninger thomas.nunninger@scaleupventures.com
www.scaleupventures.com