Implement ObjectProxy class

This commit is contained in:
Dan Jones 2021-10-15 10:43:30 -05:00
commit d857775289
10 changed files with 417 additions and 79 deletions

View file

@ -22,12 +22,8 @@ TODO: Make sure the following URLs are correct and working for your project.
## About
<!--
TODO: Use this space to provide more details about your package. Try to be
concise. This is the introduction to your package. Let others know what
your package does and how it can help them build applications.
-->
A debugging/testing tool which allows access to an object's or class's private
and protected properties and methods without the use of Reflection.
This project adheres to a [code of conduct](CODE_OF_CONDUCT.md).
By participating in this project and its community, you are expected to
@ -42,33 +38,57 @@ Install this package as a dependency using [Composer](https://getcomposer.org).
composer require danjones000/object-spy
```
<!--
## Usage
Provide a brief description or short example of how to use this library.
If you need to provide more detailed examples, use the `docs/` directory
and provide a link here to the documentation.
``` php
use Danjones000\Spy\Example;
use Danjones000\Spy\ObjectProxy;
$example = new Example();
echo $example->greet('fellow human');
$object = new Object(); // Can be any object.
$spy = new ObjectProxy($object);
// Access private property
echo $spy->privateProperty;
// Set private property
$spy->privateProperty = 42;
// Call private method
$spy->privateMethod();
// Call private method with parameters
$spy->protectedMethod(42, false);
// Run arbitry code on object, allows passing in parameters
// All arguments after the closure are passed as arguments to the closure itself
$spy->call(fn ($abc) => $this->someProp = $this->someMethod($abc) * static::MULTIPLIER, 500);
// PHP allows static methods to be called non-statically, so, this works as well
$spy->staticPrivateMethod();
// Can also access static properties/methods without an object instance
$staticSpy = new ObjectProxy(Object::class);
// If there is an Object::$privateStaticProperty
echo $staticSpy->privateStaticProperty;
$staticSpy->privateStaticProperty = 42;
$staticSpy->privateStaticMethod();
// No direct access to contstants, but we can do this
$staticSpy->call(fn () => static::PRIVATE_CONSTANT); // self also works, of course
```
-->
## Implementation notes
This class does not use reflections in its operation. Instead, if uses closures
that are bound to the instance or class.
## Contributing
Contributions are welcome! To contribute, please familiarize yourself with
[CONTRIBUTING.md](CONTRIBUTING.md).
## Copyright and License
The danjones000/object-spy library is copyright © [Dan Jones](https://danielrayjones.com/)