PHP code example of phpway / pubsubwp

1. Go to this page and download the library: Download phpway/pubsubwp 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/ */

    

phpway / pubsubwp example snippets


$pubsub = new \PubSubWP\PubSub;

$pubsub->subscribe('topic.foo', function ($event) { $event['tape'] .= 'a'; });
$pubsub->subscribe('topic.foo', function ($event) { $event['tape'] .= 'b'; });

$priority = 10;
$pubsub->subscribe('topic.foo', function ($event) { $event['tape'] .= 'A'; }, $priority);

$initialEventData = ['tape' => ''];
$event = $pubsub->publish('topic.foo', $initialEventData);

print $event['tape'];   // 'Aab'

$pubsub->subscribe(
    'topic.foo',
    function ($event) {
        $event['tape'] .= '[STOP]';
        $event->stop();
    },
    5
);

$event = $pubsub->publish('topic.foo', ['tape' => '']);
print $event['tape'];   // 'A[STOP]'

$myEvent = new MyEvent;
$event = $pubsub->publish('topic.foo', $myEvent);