PHP code example of romeoz / rock-events

1. Go to this page and download the library: Download romeoz/rock-events library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

romeoz / rock-events example snippets


use rock\events\Event;

class Foo 
{
    public $str = 'Rock!';
}

$object = new Foo();
$eventName = 'onAfter';
$handler = function (Event $event) {
    echo "Hello {$event->owner->str}"; 
};

Event::on($object, $eventName, $handler);

Event::trigger($object,  'onAfter'); // output: Hello Rock!

$handler = function (Event $event) { 
    echo "Hello Rock!"; 
};
Event::on(new Foo, 'onAfter', $handler);

Event::trigger(new Foo,  'onEvent'); 

// or

Event::trigger('test\Foo',  'onEvent');

$handler = 
    function (Event $event) {
        echo 'Hello Rock!'
    };
$instance =  new Foo;
Event::on($instance, 'onAfter', $handler);

Event::off($instance, 'onAfter');