hakito / publisher
Proxy for accessing protected/private class members.
v1.3
2020-06-20 10:39 UTC
Requires
- php: >=5.4
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-12-20 20:31:59 UTC
README
Simple proxy for accessing protected/private class members.
It's intention is to give unit tests access to private members without reinventing the wheel.
This helper uses closures to access private members of a class instead of reflection.
Installation
composer require hakito/publisher
Usage
Guess you have a class with private members:
class Target { private $foo = 'secret'; private function bar($arg) { return $arg . 'Bar'; } private static $sFoo = 'staticSecret'; }
Create the proxy to access these members:
$target = new Target(); $published = new hakito\Publisher\Published($target); // Get private property $property = $published->foo; // $property = 'secret'; // Set private property $published->foo = 'outsider'; // $target->foo = 'outsider'; // call private method $name = $published->bar('Saloon'); // $name = 'SaloonBar'; // Optional you can provide a base class in the constructor class Derived extends Target { private $foo = 'derived'; } $derived = new Derived(); $published = new hakito\Publisher\Published($derived, Target::class); $property = $published->foo; // Gets property from Target // $property = 'secret';
Accessing static members
If you want to access static fields or methods you have to use the class StaticPublished
$published = new StaticPublished(Target::class); $property = $published->sFoo; // $property = 'staticSecret'
Setting fields and calling methods works the same as for instances.
Limitations
The published method call cannot set a reference argument.
class Target { private function methodWithReferenceArgument(&$arg) { $arg = 'hi'; } } $target = new Target(); $published = new hakito\Publisher\Published($target); $val = 'initial'; $published->methodWithReferenceArgument($val); // $val is still 'initial'
The return value from method call cannot be a reference
class Target { private $_member = []; private function &methodWithReferenceReturnValue($arg) { return $this->_member; } } $target = new Target(); $published = new hakito\Publisher\Published($target); $val = 'initial'; $published->methodWithReferenceReturnValue($val)['new'] = 'value'; // Target::_member is still []