fsi / reflection
FSi Reflection Component is an extension of built-in reflection classes in PHP 5.3 that saves a lot memory, thanks to factory design pattern.
Installs: 62 502
Dependents: 2
Suggesters: 0
Security: 0
Stars: 7
Watchers: 8
Forks: 3
Open Issues: 1
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2022-01-25 14:37:25 UTC
README
FSi Reflection component is an extension of built-in reflection classes in PHP 5.3. Some of their methods are rewritten in such way that reflection objects are cached and never created twice for the same class/method/property/function or extension. This is achieved by usage of factory pattern. Some of the methods that cannot be rewriten to use such a cache throw an exception. All ReflectionProperty and ReflectionMethod objects returned from this library are previously set as accessible so they can be used even if they're private or protected.
Setup and autoloading
We highly recommend to use autoloader generated by composer.phar
Adding reflection into composer.json
{
...
"require": {
...
"fsi/reflection": "0.9.*"
...
},
...
}
Usage example
The key idea of using this reflection library is not to contruct reflection classes by their constructors but through the factory() methods added to each reflection class. Let's assume we have a class named ClassA:
<?php class ClassA { private $privateProperty; protected $protectedProperty; public $publicProperty; public function __construct($constructorParam) { } private function privateMethod($paramA, $paramB) { return $paramA . '-' .$paramB; } protected function protectedMethod($paramC, $paramD) { return $paramC . '+' .$paramD; } public function publicMethod($paramE, $paramF) { return $paramE . '=' .$paramF; } }
In order to create reflection objects you can:
<?php use FSi\Reflection\ReflectionClass; use FSi\Reflection\ReflectionProperty; use FSi\Reflection\ReflectionMethod; $reflectionClassA = ReflectionClass::factory('ClassA'); $reflectionPropertyPrivate = $reflectionClassA->getProperty('privateProperty'); $reflectionPropertyPrivate = ReflectionProperty::factory('ClassA', 'privateProperty'); $reflectionMethodPrivate = $reflectionClassA->getMethod('privateMethod'); $reflectionMethodPrivate = ReflectionMethod::factory('ClassA', 'privateMethod');
You must remember that using any reflection class' constructor directly in your code will throw an exception.