marcin-orlowski / php-lockpick
This package is abandoned and no longer maintained.
The author suggests using the marcin-orlowski/lockpick package instead.
Helps accessing protected/private members/consts of foreign objects.
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
Collection of PHP helper methods allowing easy access to protected or private properties and constants of objects or classes as well as allowing to call such non-public methods. This library is mostly useful while creating unit tests for your code.
Installation
composer require marcin-orlowski/lockpick
Usage
As all methods come as set of static methods, so you just need to add related use
to your
code class and all methods should be simply available via static reference Lockpick::...
:
use Lockpick\Lockpick; use PHPUnit\Framework\Assert; $obj = new Stronghold(); $actual = Lockpick::call($obj, 'openSessame', [ 'abracadabra' ]); Assert::assertEquals($expected, $actual); ...
Available methods
Notes:
- The
$clsOrObj
parameter can be eitherobject
or class name (string
) - The
Lockpick::call()
argument$args
now accepts single parameters w/oarray
wrapping (socall(..., $arg1)
is now valid and equivalent tocall(..., [ $arg1 ])
) as it will be automatically wrapped under the hood. - If you want to actually pass
array
as single argument to called function you must wrap it into another array, like this:call(..., [[ $arg1, $arg2 ]])
. Default is empty array which means no arguments will be passed to the called method.
Method | Description |
---|---|
call($clsOrObj, string $methodName, mixed $args): mixed | Calls object/class protected/private method |
getProperty($clsOrObj, string $name): mixed | Returns value of protected/private property |
setProperty($clsOrObj, string $name, mixed $value): mixed | Sets value of protected/private property |
getConstant($clsOrObj, string $name): mixed | Returns value of protected/private constant |
getMethodVisibility($clsOrObj, string $name): Visibility | Returns visibility of class method |
getPropertyVisibility($clsOrObj, string $name): Visibility | Returns visibility of class property |
getConstantVisibility($clsOrObj, string $name): Visibility | Returns visibility of class constant |
License
- Written and copyrighted ©2014-2024 by Marcin Orlowski
- Open-sourced software licensed under the MIT license
- ASCII Art created using https://textkool.com