PHP code example of garrettw / noair

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

    

garrettw / noair example snippets


class MyObserver extends \Noair\Observer
{
    public function onThing(\Noair\Event $e)
    {
        return 'do it ' . $e->data;
    }
}

$hub = new \Noair\Mediator(new \Noair\Manager);

$obs = (new MyObserver($hub))->subscribe();

$hub->publish(new \Noair\Event('thing', 'now'));

// Now if you're an object-oriented fiend like me, you'll probably be calling that
// from within a method, like so:
// $this->mediator->publish(new \Noair\Event('thing', 'now', $this));

// Anyway, either of those will return: 'do it now'

class OtherObserver extends \Noair\Observer
{
    public function subscribe() {
        $this->handlers = [
            'doWeirdThings' => [ // an event name that this class handles
                [$this, 'doWeirdThingsAlways'], // the callable that the event fires
                \Noair\Mediator::PRIORITY_HIGHEST, // how important this handler is
                true, // this is the forceability setting
            ],
        ];

        return parent::subscribe();
    }

    // This is just a normal handler
    public function onThing(\Noair\Event $e)
    {
        return 'do it ' . $e->data;
    }

    // Wait, this function doesn't start with "on"! How can it work?
    // See subscribe() above.
    public function doWeirdThingsAlways(\Noair\Event $e)
    {
        return 'do ' . $e->data . ' ' . rand() . ' times';
    }
}

$hub = new \Noair\Mediator();
$obs = (new OtherObserver($hub))->subscribe();

$hub->publish(new \Noair\Event('doWeirdThings', 'stuff'));