PHP code example of kovalevsky-projects / event-dispatcher

1. Go to this page and download the library: Download kovalevsky-projects/event-dispatcher 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/ */

    

kovalevsky-projects / event-dispatcher example snippets


$dispatcher = new \KovalevskyProjects\EventDispatcher\EventDispatcher();

$dispatcher->on('hello.world', function() {
  echo 'Hello World!';
});

$dispatcher->trigger('hello.world');

$dispatcher->on('hello.world', function($text) {
  echo $text;
});

$dispatcher->on('foo.bar', function($foo, $bar) {
  echo $foo + $bar;
});

$dispatcher->trigger('hello.world', 'Hello World!');

// You can use simple numeric arrays instead of associative
$dispatcher->trigger('foo.bar', array(
  'foo' => 2,
  'bar' => 2,
));

$dispatcher->trigger('foo.bar', 'some parameter');
// or
$dispatcher->trigger('foo.bar', [
  'one' => 'parameter one',
  'two' => 'parameter two',
]);

$dispatcher->on('post.update', function($post, $author) {
  notify('The post ' . $post->title . ' updated by the ' . $author);
});

$dispatcher->on('some.action', $callableFunction);
// ...
$dispatcher->off('some.action', $callableFunction);

$dispatcher->bind('foo bar baz', function() {
  echo 'You will see this message when foo, bar and baz events will be triggered';
});

// or with array

$dispatcher->bind(['foo', 'bar', 'baz'], function() { ... });

// or you can specify different actions for the each event

$dispatcher->bind([
  'foo' => function() { ... },
  'bar' => function() { ... },
  'baz' => function() { ... },
]);

$dispatcher->unbind('foo bar baz');
// or
$dispatcher->unbind(['foo', 'bar', 'baz']);
// or you can remove ALL actions.
$dispatcher->unbind();

$dispatcher->has('foo');
// or you can check for the specific action
$dispatcher->has('foo', $someAction);