php-events

Created: 2012-03-26 10:33
Updated: 2014-12-13 11:29
php

README.md

PHP-Events is a php events system for PHP 5.4

It's based on traits, which means every class can implement the event system as easily as:

class MyClass {
    use Events;
}

Binding and triggering events

$class = new MyClass();
$class->bind("boom", function() {
    echo "headshot";
});
$class->trigger("boom");

You can also pass in arguments:

$class = new MyClass();
$class->bind("hello", function($name) {
    echo "hello " . $name;
});
$class->trigger("hello", "Peeter");

This also supports priority and stopping event propagation. By default, all events are triggered in the order they are bound. You can change that by assiging a priority to an event, and returning false to stop propagation.

$class = new MyClass();
$class->bind("hello", function($name) {
    echo "hello " . $name;
});
$class->bind("hello", function($name) {
    echo "hello my dear sir " . $name;
    return false;
    
}, 2);
$class->trigger("hello", "Peeter");

Would produce

hello my dear sir Peeter
Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more