PHP Event API

A simple Event API with Event chaining and Triggers.

Categories: Development

Events

I tried a couple of existing plugin solutions most did 95% of what I needed and the other 5% was next to impossible to force.

After wasting days I decided I would write my own, what follows is an overview of Tentacle event API.

Contact me if you have any questions.

Registering an event:

function method_one (){
    echo 'one ';
}

function method_two (){
    echo 'two ';
}

event::on('event_trigger', 'method_one', 2);
event::on('event_trigger', 'method_two', 1);

event::trigger('event_trigger');

Turning events off:

event::off('event_trigger');
event::off('event_trigger', 'method_two');
event::off(null, 'method_one');

Test if an event exists:

var_dump(event::exists('event_trigger'));

boolean true

Trigger an event:

function method_name ( )
{
    echo 'my method name';
}

event::on('event_name', 'method_name');

event::trigger('event_name');

my method name

Trigger an event and pass data to it:

function method_data ( $text = '' )
{
    echo ' 1  my method data is '.$text;
}

function method_data_two ( $text = '' )
{
    echo ' 2 my method data is '.$text;
}

event::on('event_data', 'method_data', 1);
event::on('event_data', 'method_data_two', 2);


event::trigger('event_data', 'this');

1 my method data is this 2 my method data is this

Triggering a class and passing data to the method

class my
{
    static function method_name ( $text = '' )
    {
        echo 'my class method name is '.$text;
    }
}

event::on('event_class', 'my::method_name');

event::trigger('event_class', 'Lary');

my class method name is Lary

Event chaining

function method_sad ( $text = '' )
{
    return str_replace('blah', "sad", $text);
}

function method_happy ( $text = '' )
{
    return str_replace('sad', "happy", $text);
}

event::on('event_mood', 'method_sad', 1);
event::on('event_mood', 'method_happy', 2);

echo event::filter('event_mood', 'I am blah!');

I am happy!


Adam Patterson

Adam Patterson

User Interface Designer & Developer with a background in UX. I have spent 5 years as a professionally certified bicycle mechanic and ride year-round.

I am a husband and father of two, I enjoy photography, music, movies, coffee, and good food.

You can find me on Twitter, Instagram, and YouTube!