承接 devco/event-emitter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

devco/event-emitter

Composer 安装命令:

composer require devco/event-emitter

包简介

Port of NodeJSs EventEmitter to PHP 5.4 using traits

README 文档

README

This is a direct port of the EventEmitter class from node.js using PHP 5.4 traits.

Why PHP 5.4?

Due to the nature of the EventEmitter functionnality, using simple extends sounds like the wrong way to deal with it as it is a set of functionnality to add to an existing class instead of your class extending the functionnalities of EventEmitter. As such traits are the best way to implement this kind of functionnality and those are only available to PHP 5.4+.

How to Use

<?php

// if using directly
require_once('EventEmitter.php');

// if using through composer just use the autoload
require_once('vendor\.composer\autoload.php');

// test class
class Item {
  use \Nekoo\EventEmitter;

  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'));

API

  • setMaxListeners(int)
    Set the maximum listeners allowed by events, default is 10. Adding too many listeners to a same event on the same object will make fireing events slower and slower, just up this limit if needed.
<?php
$object->setMaxListeners(20);
<?php
$object->emit('event', $foo, $bar);
  • on(string, callable)
    Register a callback for en event, every forms of callbacks are accepted (strings, arrays or closures).
<?php
$object->on('event', function() { var_dump(func_get_args()); });
<?php
$object->all(function($event, $arg1) { var_dump($event, $arg1); });
  • once(string, callable)
    Same thing as on() but the listener will only be called once.
  • off(string, callable)
    Removes a listener for this event. You need to provide the very same array or string, or if using a closure the same instance.
<?php

$fun = function($arg1) { echo "event: $arg1\n"; };
$object->on('event', $fun); // adds the event
$object->off('event', $fun); // remove the event
<?php
$object->removeAllListeners('event'); // only for the 'event' event
$object->removeAllListeners(); // for every events on the object
<?php
foreach ($object->getListeners('event') as $listener) {
  var_dump($listener);
}

统计信息

  • 总下载量: 112.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 7
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-09-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固