PHP code example of devco / event-emitter

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

    

devco / event-emitter example snippets




// if using directly
er just use the autoload
r;

  public function register($infos) {
    // do something
    // fire the event
    $this->emit('register', $infos);
  }
}

// allows you to check if a class uses the event emitter through
// $class instanceof \Nekoo\EventEmitterInterface
class AnotherItem implements \Nekoo\EventEmitterInterface {
  use \Nekoo\EventEmitter;
}

// create an instance of our object
$i = new Item();
// register an observer for the register event
$i->on('register', function($infos) {
    echo "Item registered!\n";
    var_dump($infos);
});
// call the method
$i->register(array('key' => 'value'));


$object->setMaxListeners(20);


$object->emit('event', $foo, $bar);


$object->on('event', function() { var_dump(func_get_args()); });


$object->all(function($event, $arg1) { var_dump($event, $arg1); });



$fun = function($arg1) { echo "event: $arg1\n"; };
$object->on('event', $fun); // adds the event
$object->off('event', $fun); // remove the event


$object->removeAllListeners('event'); // only for the 'event' event
$object->removeAllListeners(); // for every events on the object


foreach ($object->getListeners('event') as $listener) {
  var_dump($listener);
}