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.
$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;
}
});
- PHP 5.4
- Composer
You can install it via Composer, and use it instantly thanks to the Composer autoloader:
"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
If you have any comments, ideas, questions... don't hesitate to raise an issue in GitHub :-)
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request