dev-master
9999999-dev http://github.com/isra00/class-triggersInject magic actions before and after any method of any class
LGPL v3.0 LGPL-3.0-or-later
The Requires
- php >=5.4
by Israel Viana
magic classes objects methods aop triggers
Wallogit.com
2017 © Pedro Peláez
Inject magic actions before and after any method of any class
Partial and experimental implementation of Aspect-Oriented Programming. It gives you the ability to capture and manipulate the inputs, outputs and execution of any method of any class in PHP., (*2)
$a = new AnyClass;
$a = new ClassTriggers($a);
$a->bind($targetMethod, $trigger, function($arguments) {
//Injected logic.
});
use ClassTriggers\ClassTriggers;
class Car
{
private $position = 0;
public function forward($increment)
{
$this->position += $increment;
}
public function getPosition()
{
return $this->position;
}
}
$interceptedCar = new ClassTriggers(new Car);
$interceptedCar->bind('forward', 'preMethod', function(&$arguments) {
echo 'You have requested forward(' . $arguments[0] . ")\n";
if ($this->position + $arguments[0] > 10) {
echo "Can't forward more than 10. Stopping method execution.\n";
return ClassTriggers::COND_STOP_EXECUTION;
}
});
You can install it via Composer, and use it instantly thanks to the Composer autoloader:, (*3)
"require": {
"isra00/class-triggers": "dev-master"
}
I wrote a tutorial in Spanish: https://github.com/isra00/blog/blob/master/2012-12-14-inyectar-logica-en-los-metodos-de-cualquier-clase-php-dinamicamente.md, (*4)
If you have any comments, ideas, questions... don't hesitate to raise an issue in GitHub :-), (*5)
git checkout -b my-new-feature)git commit -am 'Add some feature')git push origin my-new-feature)Inject magic actions before and after any method of any class
LGPL v3.0 LGPL-3.0-or-later
magic classes objects methods aop triggers