derywat/php-events
Composer 安装命令:
composer require derywat/php-events
包简介
PHP library for sending and receiving events.
README 文档
README
Library allows to send events from class to all registered receivers.
Sending event from class
Define custom event class
Example class implements sending string message.
use derywat\events\EventInterface; class MyCustomEventClass implements EventInterface { protected $message; public function __construct(string $message){ $this->message = $message; } public function __tostring(): string { return "{$this->message}"; } }
Send event
Use custom event class to report event to all receivers.
use derywat\events\EventsProducerInterface; use derywat\events\EventsProducerTrait; class MyClass implements EventsProducerInterface { use EventsProducerTrait; protected function myMethodWithEvent(){ //report event $this->reportEvent(new MyCustomEventClass("message to send in event")); } }
Receiving events
Receiving events in class
Events may be received in any class by implementing EventsReceiverInterface.
use derywat\events\EventsReceiverInterface; class MyEventReceivingClass implements EventsReceiverInterface { public function eventHandler(EventInterface $event):void { $class = get_class($event); switch($class) { case MyCustomEventClass::class: //handle events of MyCustomEventClass here break; } } }
Multiple objects of classes implementing EventsReceiverInterface may be added using registerEventReceiver method.
$receiverInstance = new MyEventReceivingClass(); $producerInstance = new MyClass(); //register receiver in producer $producerInstance->registerEventReceiver($receiverInstance);
Receiving events in closures defined outside of class instance
Adding EventReceiverTrait implements EventsReceiverInterface in any class.
use derywat\events\EventsReceiverInterface; use derywat\events\EventReceiverTrait; class MyEventReceivingClass implements EventsReceiverInterface { use EventReceiverTrait; }
Trait implements event handler using event handling closures. Event handlers are external to class and class instance object.
use derywat\events\EventsReceiver; $receiverInstance = new MyEventReceivingClass(); $receiverInstance->addEventHandlerClosure( function(EventInterface $event):void { $class = get_class($event); switch($class) { case MyCustomEventClass::class: //handle events of MyCustomEventClass here break; } } ); $producerInstance = new MyClass(); $producerInstance->registerEventReceiver($receiverInstance);
Using EventsReceiver with EventsReceiver object
Event handling can be implemented with instance of predefined EventsReceiver class.
use derywat\events\EventsReceiver; $producerInstance = new MyClass(); $producerInstance->registerEventReceiver( //create instance of EventsReceiver (new EventsReceiver())->addEventHandlerClosure( function(EventInterface $event):void { $class = get_class($event); switch($class) { case MyCustomEventClass::class: //handle events of MyCustomEventClass here break; } } ) );
derywat/php-events 适用场景与选型建议
derywat/php-events 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 derywat/php-events 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 derywat/php-events 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-11