承接 eso/ireflection 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

eso/ireflection

最新稳定版本:v0.1.3

Composer 安装命令:

composer require eso/ireflection

包简介

Wrapper around PHP Reflection to make it easier to use

README 文档

README

Latest Stable Version Build Status Coverage Status

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

All contributors

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

GitHub 信息

  • Stars: 5
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-12-07