PHP code example of messy-one / mediator

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

    

messy-one / mediator example snippets


   // create an instance
   $mediator = new Mediator();
   
   // create an class implementing the EventData interface
   class ConcreteEventData implements EventData
   {
       /** @var string */
       private $foo;
   
       /**
        * @param string $foo
        */
       public function __construct($foo)
       {
           $this->foo = $foo;
       }
   
       /**
        * @return string
        */
       public function getFoo()
       {
           return $this->foo;
       }
   }

   
   // attach an event
   $mediator->attach('unique:event', function ($event, ConcreteEventData $data) {
      // do whatever you have to do with $data->getFoo()
   });
   
   // somewhere else in the code you can trigger the event and send the data to the callback function
   $mediator->trigger('unique:event', new ConcreteEventData('foo'));