PHP code example of tomwright / eventing

1. Go to this page and download the library: Download tomwright/eventing library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tomwright / eventing example snippets


namespace App\Eventing\Event;

class UserWasRegistered extends \TomWright\Eventing\Event\Event
{
	protected $userId;
    
    public function setUserId($userId)
    {
    	$this->userId = $userId;
    }
    
    public function getUserId()
    {
    	return $this->userId;
    }
}

namespace App\Eventing\Handler;

class UserWasRegisteredHandler implements \TomWright\Eventing\Listener\ListenerInterface
{   
    public function handle(\TomWright\Eventing\Event\EventInterface $event)
    {
    	echo "User #{$event->getUserId()} has been registered.";
    }
}

$bus = \TomWright\Eventing\EventBus::getInstance();
$bus->addListenerNamespace('\\App\\Eventing\\Handler');

$bus = \TomWright\Eventing\EventBus::getInstance();
$event = new \App\Eventing\Event\UserWasRegistered();
$event->setUserId(123);
$bus->dispatch($event);