marcin-orlowski/php-lockpick

此包已被废弃,不再维护。作者建议使用 marcin-orlowski/lockpick 包。

帮助访问外部对象的受保护/私有成员/常量。

1.4.0 2024-02-07 13:53 UTC

This package is auto-updated.

Last update: 2024-02-07 13:53:40 UTC


README

 ▄█        ▄██████▄   ▄████████    ▄█   ▄█▄    ▄███████▄  ▄█   ▄████████    ▄█   ▄█▄
███       ███    ███ ███    ███   ███ ▄███▀   ███    ███ ███  ███    ███   ███ ▄███▀
███       ███    ███ ███    █▀    ███▐██▀     ███    ███ ███▌ ███    █▀    ███▐██▀  
███       ███    ███ ███         ▄█████▀      ███    ███ ███▌ ███         ▄█████▀  
███       ███    ███ ███        ▀▀█████▄    ▀█████████▀  ███▌ ███        ▀▀█████▄  
███       ███    ███ ███    █▄    ███▐██▄     ███        ███  ███    █▄    ███▐██▄  
███▌    ▄ ███    ███ ███    ███   ███ ▀███▄   ███        ███  ███    ███   ███ ▀███▄
█████▄▄██  ▀██████▀  ████████▀    ███   ▀█▀  ▄████▀      █▀   ████████▀    ███   ▀█▀
▀                                 ▀                                        ▀  

Lockpick

Latest Stable Version License

PHP辅助方法集合,允许轻松访问对象或类的受保护或私有属性和常量,并允许调用此类非公共方法。此库在编写代码的单元测试时非常有用。

安装

composer require marcin-orlowski/lockpick

使用

由于所有方法都作为一组 静态 方法提供,因此您只需将相关的 use 添加到您的代码类中,所有方法都应通过静态引用 Lockpick::... 简单地可用。

use Lockpick\Lockpick;
use PHPUnit\Framework\Assert;

$obj = new Stronghold();
$actual = Lockpick::call($obj, 'openSessame', [ 'abracadabra' ]);
Assert::assertEquals($expected, $actual);
...

可用方法

说明

  • $clsOrObj 参数可以是 object 或类名(string
  • Lockpick::call() 参数 $args 现在接受单个参数,无需 array 包装(因此 call(..., $arg1) 现在是有效的,与 call(..., [ $arg1 ]) 等价),因为它将在底层自动包装。
  • 如果您想将 array 作为单个参数传递给调用函数,则必须将其包装在另一个数组中,例如:call(..., [[ $arg1, $arg2 ]])。默认为空数组,表示不会传递任何参数给调用方法。
方法 描述
call($clsOrObj, string $methodName, mixed $args): mixed 调用对象/类的受保护/私有方法
getProperty($clsOrObj, string $name): mixed 返回受保护/私有属性的值
setProperty($clsOrObj, string $name, mixed $value): mixed 设置受保护/私有属性的值
getConstant($clsOrObj, string $name): mixed 返回受保护/私有常量的值
getMethodVisibility($clsOrObj, string $name): Visibility 返回类方法的可见性
getPropertyVisibility($clsOrObj, string $name): Visibility 返回类属性的可见性
getConstantVisibility($clsOrObj, string $name): Visibility 返回类常量的可见性

许可证