Download the PHP package vedavith/pops-fork without Composer
On this page you can find all versions of the php package vedavith/pops-fork. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package pops-fork
Pops
PHP Object Proxy System.
This is a maintained fork of eloquent/pops originally created by Erin Millard.
Installation and documentation
- Available as Composer package
vedavith/pops-fork(maintained fork ofeloquent/pops). - Requires PHP 8.2 or later.
What is Pops?
Pops is a system for wrapping PHP objects in other objects to modify their behaviour. A Pops proxy will, as much as possible, imitate the object it wraps. It passes along method calls and returns the underlying result, and allows transparent access to properties (for both setting and getting).
Pops is the underlying system behind Liberator.
Creating proxies
Let's write a simple proxy that converts everything to uppercase. Here we have a class:
And here is our proxy:
We use popsCall() here rather than __call() to get around PHP limitations to
do with passing arguments by reference. See [calling methods with by-reference
parameters] for a deeper explanation.
Now when we access wat() and $derp both normally, and through our proxy, we
can see the effect:
Recursive proxies
Pops proxies can be applied to any value recursively. This comes in handy when designing, for example, an output escaper (similar to Symfony). Here's an example of how such a system could be created for escaping HTML output:
The output escaper can now be used like so:
Which would output:
Note that the above example should NOT be used in production. Output escaping is a complex issue that should not be taken lightly.
Excluding values from recursion
Note that in the above example, the last list item was wrapped in a Safe proxy. When Pops applies its proxies, it will skip anything marked as safe in this manner.
Calling methods with by-reference parameters
Because of PHP limitations, methods with arguments that are passed by reference must be called in a special way.
To explain futher, let's assume our class from before also has a method which accepts a reference:
This method cannot be proxied normally because the $wasPhone argument is
passed by reference. The correct way to call the above butWho() method through a
Pops proxy looks like this:
Note that there must be a variable for the $wasPhone argument, and there
must be a variable for the arguments themselves. Neither can be passed
directly as a value. The arguments must also contain a reference to
$wasPhone argument.