eso/ireflection
最新稳定版本:v0.1.3
Composer 安装命令:
composer require eso/ireflection
包简介
Wrapper around PHP Reflection to make it easier to use
关键字:
README 文档
README
Wrapper around PHP Reflection to make it easier to use.
Requirements
- PHP >= 5.4
Installation
The recommended way to install is through composer.
Just create a composer.json file for your project:
{
"require": {
"eso/ireflection": "@stable"
}
}
Tip: browse eso/ireflection page to choose a stable version to use, avoid the @stable meta constraint.
And run these two commands to install it:
$ curl -sS https://getcomposer.org/installer | php
$ composer install
Now you can add the autoloader, and you will have access to the library:
<?php require 'vendor/autoload.php';
Usage
Get/set some properties using Reflection:
class A { protected $a = 5; protected $b = 10; } $a = new A(); $refClass = new \ReflectionClass($a); $propA = $refClass->getProperty('a'); $propB = $refClass->getProperty('b'); $propA->setAccessible(true); $propB->setAccessible(true); echo $propA->getValue($a) . "\n"; // prints 5 echo $propB->getValue($a) . "\n"; // prints 10 $propA->setValue($a, 10); $propB->setValue($a, 20); echo $propA->getValue($a) . "\n"; // prints 10 echo $propB->getValue($a) . "\n"; // prints 20
Get/set the same properties using ReflClass:
class A { protected $a = 5; protected $b = 10; } $a = new A(); $refClass = ReflClass::create($a); echo $refClass->getAnyPropertyValue('a') . "\n"; // prints 5 echo $refClass->getAnyPropertyValue('b') . "\n"; // prints 10 $refClass->setAnyPropertiesValues(array('a' => 10, 'b' => 20)); echo $refClass->getAnyPropertyValue('a') . "\n"; // prints 10 echo $refClass->getAnyPropertyValue('b') . "\n"; // prints 20
Other example, invoke a method:
class A { function sum($a, $b) { return $a + $b; } } $a = new A(); $method = (new \ReflectionClass($a))->getMethod('sum'); $method->setAccessible(true); echo $method->invokeArgs($a, array(5, 3)) . "\n"; // prints 8
Using ReflClass:
class A { function sum($a, $b) { return $a + $b; } } $a = new A(); echo ReflClass::create($a)->invokeAnyMethod('sum', array(10, 3)); // prints 8
Contributing
See CONTRIBUTING file.
License
IReflection library is released under the MIT License. See the bundled LICENSE file for details.
统计信息
- 总下载量: 202.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 1
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-12-07