best-served-cold/reflection

反射 - 一个简单的实用工具,用于将对象和类反射到属性和方法。

1.0.3 2017-03-14 21:14 UTC

This package is not auto-updated.

Last update: 2024-09-28 19:42:10 UTC


README

Build Status Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight Latest Stable Version Code Climate Issue Count

反射

通过重载简单查询私有方法和属性。

安装

composer require best-served-cold/reflection

使用方法

这个类

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;
    }
}

作为类的使用

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

echo $reflectionClass->protectedStaticProperty . PHP_EOL;
echo $reflectionClass->privateStaticProperty . PHP_EOL;
echo $reflectionClass->protectedStaticMethod(2) . PHP_EOL;
echo $reflectionClass->privateStaticMethod(4) . PHP_EOL;

返回值

2
4
5
8

作为对象的使用

$reflectionObject = new ReflectionObject(new Exampleclass);

echo $reflectionObject->protectedProperty . PHP_EOL;
echo $reflectionObject->privateProperty . PHP_EOL;
echo $reflectionObject->protectedMethod(2) . PHP_EOL;
echo $reflectionObject->privateMethod(4) . PHP_EOL;

返回值

1
3
3
6