hasan-22/event-listener
最新稳定版本:v1.0.0
Composer 安装命令:
composer require hasan-22/event-listener
包简介
With this package, you can easily add and use event and listener to your project
README 文档
README
With this package, you can easily add and use event and listener to your project
composer require hasan-22/event-listener
Usage:
You can create a class for the event and a class for the listener.
Note that the Listener class must be implemented from the \EventListener\ListenerInterface interface
In this example, we think we have two event listener classes for login
Our classes name is LoginEvent and LoginListener
require_once __DIR__.'/vendor/autoload.php'; //With function listen we can add register our event and listener \EventListener\Event::listen(LoginEvent::class, LoginListener::class); // Now we can fire events anywhere in the project like this \EventListener\Event::fire(new LoginEvent()); // If your event have constructor with parameters you can pass the parameters like this // Be careful that your parameters must have public access \EventListener\Event::fire(new LoginEvent('Armia','123456'));
If you don't want to create class for listener and event, we can create event and listener in this way
\EventListener\Event::listen('login',function(){ echo 'user is login'; }) \EventListener\Event::fire('login'); // With parameter \EventListener\Event::listen('login',function($username){ echo "Hello $username"; }) \EventListener\Event::fire('login','Armia');
If you have multiple events and listeners, you can listen or fire them one after the other
\EventListener\Event::listen('event_one',function($name){ echo "Hello $name"; })->listen(EventTwo::class, ListenerTwo::class)->listen('event_three', function(){}); // Fire events \EventListener\Event::fire('event_one','Armia')->fire(new EventTwo())->fire('event_three');
If you want to delete the event, do so
\EventListener\Event::delete('event_name');
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-13