Skip to content

malisetti/reflection

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reflection - Optimized reflection package for PHP 5.3

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.

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published