承接 derywat/php-events 相关项目开发

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

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

derywat/php-events

最新稳定版本:0.1.0

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-11