dhenfie/event-dispatcher
最新稳定版本:1.0.0
Composer 安装命令:
composer require dhenfie/event-dispatcher
包简介
Simple PHP Event Dispatcher
README 文档
README
This is a simple event dispatcher for PHP. It allows components of an application to communicate with each other through events without having to know anything about each other.
Install
composer require dhenfie/event-dispatcher
Usage
# Register Listeners
A listener is something that listens to events and does something when the event occurs. To register a listener, use the EventDispatcher::listen($eventName, $listener) method.
A listener is a class that implements the EventListenerInterface interface.
Here is an example class as an event listener:
<?php // file SendToEmail.php use Dhenfie\EventDispatcher\EventDispatcher; class SendToEmail implements EventListenerInterface { public function handle(array $params = []) { // send an email to the user Mail::send($params['email'], $params['message']); } }
Next, register the listener class using the `listen() method:
<?php use Dhenfie\EventDispatcher\EventDispatcher; use SendToEmail; // daftarkan pendengar menggunakan static method `listen` EventDispatcher::listen('passwordRequestSend', new SendToEmail());
When the passwordRequestSend event occurs, the handle() method on the SendToMail object will be executed.
Trigger Events
Trigger a specific event to occur using the dispatch() method.
example:
<?php use Dhenfie\EventDispatcher\EventDispatcher; // trigger event passwordRequestSend EventDispatcher::dispatch('passwordRequestSend');
The dispatch method also accepts a second parameter, which is an array that can be used to send additional information to the listener.
Example:
<?php use Dhenfie\EventDispatcher\EventDispatcher; EventDispatcher::dispatch('passwordRequestSend', ['email' => 'example@mail.com', 'message' => 'Your password reset token we send your email']);
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-25