Download the PHP package leedavis81/altr-ego without Composer
On this page you can find all versions of the php package leedavis81/altr-ego. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download leedavis81/altr-ego
More information about leedavis81/altr-ego
Files in leedavis81/altr-ego
Package altr-ego
Short Description Access an objects protected / private properties and methods
License MIT
Informations about the package altr-ego
Create an AltrEgo
A tool to allow you access to an object's protected / private properties by breaking PHP scope. This is useful in testing scenarios where you want to quickly verify a hidden encapsulated routine within your application. Often when you just want to test a small part of your application, that routine can be (rightly so) encapsulated and set to private / protected. And is uncallable from your test suite. Running through the process of creating, mocking and injecting dependencies just to get your object in a valid state for your test can be very timely. This tool will help you call that private routine or inspect that protected property directly.
AltrEgo allows you to completely maintain your object's state throughout any manipulations. If you decide you want the scope to come back into play, you simply fetch your object back. Any changes made during its time as an "AltrEgo" object will remain.
This library uses adapters for different versions of PHP.
If you're using 5.3
Then PHP's Reflection classes are used to break the scope of your object.
For 5.4 this library uses closure scope binding
PHP 5.4 has a new "scope breaking" feature with the use of closures. Take a look at Davey Shafik's closure puzzle blog post top get a good understanding of how this works. This method is far quicker than using PHP's built in Reflection tools. Tests I've performed "breaking scope" this way have given a speed increase in of around 52%.
Usage
Limitations
- Whenever accessing an array property it will be converted (and maintained) as an ArrayObject. This is due to limitation on setting array values when using PHP overloading (__get). If this is a problem you can either get the value directly, then overwrite it with a modified one. Or once you've retrieved the modified ArrayObject simply run ->toArray() on it.