marcin-orlowski / php-lockpick
1.4.0
2024-02-07 13:53 UTC
Requires
- php: ^8.1
- marcin-orlowski/type-asserts: ^2.0.0
Requires (Dev)
- phpstan/phpstan: ^1.8
- phpunit/php-code-coverage: ^9.0
- phpunit/phpunit: ^9.0
Replaces
README
▄█ ▄██████▄ ▄████████ ▄█ ▄█▄ ▄███████▄ ▄█ ▄████████ ▄█ ▄█▄
███ ███ ███ ███ ███ ███ ▄███▀ ███ ███ ███ ███ ███ ███ ▄███▀
███ ███ ███ ███ █▀ ███▐██▀ ███ ███ ███▌ ███ █▀ ███▐██▀
███ ███ ███ ███ ▄█████▀ ███ ███ ███▌ ███ ▄█████▀
███ ███ ███ ███ ▀▀█████▄ ▀█████████▀ ███▌ ███ ▀▀█████▄
███ ███ ███ ███ █▄ ███▐██▄ ███ ███ ███ █▄ ███▐██▄
███▌ ▄ ███ ███ ███ ███ ███ ▀███▄ ███ ███ ███ ███ ███ ▀███▄
█████▄▄██ ▀██████▀ ████████▀ ███ ▀█▀ ▄████▀ █▀ ████████▀ ███ ▀█▀
▀ ▀ ▀
Lockpick
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 | 返回类常量的可见性 |
许可证
- 由 Marcin Orlowski 编写并拥有版权 ©2014-2024
- 开源软件,根据 MIT 许可证 许可
- ASCII Art 由 https://textkool.com 创建