v1.0 2015-06-04 08:29 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:15:54 UTC


README

本包添加了用于与PHP 反射一起工作的辅助方法

安装

在您的 composer.json 中添加 FivePercent/Reflection

{
    "require": {
        "fivepercent/reflection": "~1.0"
    }
}

现在运行以下命令让 composer 下载库

$ php composer.phar update fivepercent/reflection

基本用法

FivePercent\Component\Reflection\Reflection 是最终的,不能作为对象使用。该类中的所有方法都是静态的。

本包使用示例

use FivePercent\Component\Reflection\Reflection

// Get class reflection
$reflection = Reflection::loadClassReflection('MyClass');

// Get method reflection
$reflection = Reflection::loadMethodReflection('MyClass', 'myMethod');

// Get object reflection
$object = new MyClass();
$reflection = Reflection::loadObjectReflection($object);

// Get all properties from class
$properties = Reflection::getClassProperties('MyClass');

// Get all properties from class with gets from parents classes
$properties = Reflection::getClassProperties('MyClass', true);

// Get only private properties from class
$properties = Reflection::getClassProperties('MyClass', false, \ReflectionProperty::IS_PRIVATE);

// Set value to property in object
$object = new MyClass();
Reflection::setPropertyValue($object, 'myProperty', 'FooBar');

// Get value from property
$object = new MyClass();
$value = Reflection::getPropertyValue($object, 'myProperty');