PHP code example of 68publishers / event-dispatcher-extra

1. Go to this page and download the library: Download 68publishers/event-dispatcher-extra 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/ */

    

68publishers / event-dispatcher-extra example snippets




use Symfony\Contracts\EventDispatcher\Event;
use SixtyEightPublishers\EventDispatcherExtra\EventDispatcherAwareTrait;
use SixtyEightPublishers\EventDispatcherExtra\EventDispatcherAwareInterface;

final class MyService implements EventDispatcherAwareInterface
{
    use EventDispatcherAwareTrait;

    public function doSomething() : void
    {
        $this->getEventDispatcher()->dispatch(new Event(), 'something');
    }
}

interface MyServiceFactoryInterface
{
    public function create() : MyService;
}



/** @var MyServiceFactoryInterface $factory */

$service1 = $factory->create();
$service2 = $factory->create();

# attach a local event listener on specific instance:
$service1->getEventDispatcher()->addListener('something', static function () {
    # do some stuff here
});

# the "global" listeners and subscribers will be called as first and then will be called "local" listener defined above:
$service1->doSomething();

# only the "global" listeners and subscribers will be called:
$service2->doSomething();
bash
$ vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run