Skip to content

nark3d/Reflection

Repository files navigation

Build Status Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight Latest Stable Version

Reflection

A simple way of interrogating private methods and properties via overloading.

Install

composer require best-served-cold/reflection

Usage

Take this class:

class ExampleClass
{
    protected $protectedProperty = 1;
    protected static $protectedStaticProperty = 2;
    private $privateProperty = 3;
    private static $privateStaticProperty = 4;
    
    protected function protectedMethod($number)
    {
        return $number + 1;
    }

    private function privateMethod($number)
    {
        return $number + 2;
    }

    protected static function protectedStaticMethod($number)
    {
        return $number + 3;
    }

    private static function privateStaticMethod($number)
    {
        return $number + 4;
    }
}

Usage as a class

$reflectionClass = new ReflectionClass(ExampleClass::class);

var_dump($reflectionClass->protectedStaticProperty);
var_dump($reflectionClass->privateStaticProperty);
var_dump($reflectionClass->protectedStaticMethod(2));
var_dump($reflectionClass->privateStaticMethod(4));

Returns:

int(2)
int(4)
int(5)
int(8)

Usage as an object

$reflectionObject = new ReflectionObject(new Exampleclass);

var_dump($reflectionObject->protectedProperty);
var_dump($reflectionObject->privateProperty);
var_dump($reflectionObject->protectedMethod(2));
var_dump($reflectionObject->privateMethod(4));

Returns:

int(1)
int(3)
int(3)
int(6)

About

Simple reflection accessor

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages