nsp-team/reflection-tool

允许直接访问受保护/私有属性并调用受保护/私有方法

1.0.0 2021-11-04 03:24 UTC

This package is auto-updated.

Last update: 2024-10-04 12:08:30 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

A PHP reflection library to directly access protected/private properties and call protected/private methods.

This library works with major versions of PHP 7.3.

Installation

composer require nsp-team/reflection-tool:~1.0.0

Sample Usage

require __DIR__ . '/vendor/autoload.php';

use NspTeam\Reflection\ReflectionObject;

class Test
{
    private $key;
    private static $keyStatic;

    /**
     * 
     * @return string
     */
    private function one(): string
    {
        return '私有方法';
    }

    /**
     * @param int $i
     * @param int $j
     * @return string
     */
    private static function oneStatic(int $i, int $j): string
    {
        return "私有静态方法 带参 $i$j";
    }
}

$test = new Test();

ReflectionObject::setProperty(Test::class, 'keyStatic', 'another value');
ReflectionObject::setProperty($test, 'key', 'value ');

var_dump(ReflectionObject::callMethod($test, 'one'));
var_dump(ReflectionObject::getProperty($test, 'key'));

var_dump(ReflectionObject::callMethod($test, 'oneStatic', array(1, 2)));
var_dump(ReflectionObject::getProperty($test, 'keyStatic'));

var_dump(ReflectionObject::findProperty($test, 'key'));
var_dump(ReflectionObject::getMethod(Test::class, 'one')->getDocComment());