项目作者: scaleupstack

项目描述 :
A performance-improved, and convenient way to deal with PHP Reflection classes.
高级语言: PHP
项目地址: git://github.com/scaleupstack/reflection.git
创建时间: 2019-02-17T20:32:44Z
项目社区:https://github.com/scaleupstack/reflection

开源协议:Other

下载


ScaleUpStack/Reflection

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

    • retrieve PHP Reflection classes,
    • getting/setting static and non-static properties of an object or class,
    • invoking static and non-static methods of an object or class.

Installation

Use Composer to install this library:

  1. $ composer require scaleupstack/reflection

Usage

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

    1. use ScaleUpStack\Reflection\Reflection;
  • Methods to retrieve PHP Reflection objects via class name or object:

    1. Reflection::classByName(string $className) : \ReflectionClass
    2. Reflection::classByObject(object $object) : \ReflectionClass
    3. Reflection::propertyOfClass(string $className, string $propertyName) : \ReflectionProperty
    4. Reflection::propertyOfObject(object $object, string $propertyName) : \ReflectionProperty
    5. Reflection::allPropertiesOfClass(string $className) : \ReflectionProperty[]
    6. Reflection::allPropertiesOfObject(object $object) : \ReflectionProperty[]
    7. Reflection::methodOfClass(string $className, string $methodName) : \ReflectionMethod
    8. Reflection::methodOfObject(object $object, string $methodName) : \ReflectionMethod
    9. Reflection::allMethodsOfClass(string $className) : \ReflectionMethod[]
    10. Reflection::allMethodsOfObject(object $object) : \ReflectionMethod[]

    Please note that classByObject() returns a \ReflectionClass and not a \ReflectionObject.

  • Methods to access static and non-static properties:

    1. Reflection::getPropertyValue(object $object, string $propertyName) : mixed
    2. Reflection::getStaticPropertyValue(string $className, string $propertyName) : mixed
    3. Reflection::setPropertyValue(object $object, string $propertyName, $value) : void
    4. Reflection::setStaticPropertyValue(string $className, string $propertyName, $value) : void
  • Methods to invoke static and non-static methods:

    1. Reflection::invokeMethod(object $object, string $methodName, array $arguments) : mixed
    2. Reflection::invokeStaticMethod(string $className, string $methodName, array $arguments) : mixed

Current State

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.

Contribute

Thanks that you want to contribute to ScaleUpStack/Reflection.

License

Please check LICENSE.md in the root dir of this package.

ScaleUpVentures Gmbh, Germany

Thomas Nunninger thomas.nunninger@scaleupventures.com

www.scaleupventures.com