PHP code example of unique / events

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

    

unique / events example snippets


    class FileLoadEvent implements \unique\events\interfaces\EventObjectInterface {
        
        use \unique\events\traits\EventObjectTrait;
        
        public ? string $contents = null;
    }

    class FileLoader implements \unique\events\interfaces\EventHandlingInterface {
        
        const EVENT_AFTER_LOAD = 'after_load';
        
        use \unique\events\traits\EventTrait;
        
        public function load() {
            
            // ...some logic to load the file and set $contents value
            $event_object = new FileLoadEvent();
            $event_object->contents = $contents;
            
            $this->trigger( self::EVENT_AFTER_LOAD, $event_object );
            return $event_object->getHandled();
        }
    } 

    $obj = new FileLoader();
    $obj->on( FileLoader::EVENT_AFTER_LOAD, function ( FileLoadEvent $event_object ) {
        
        if ( $event_object->contents !== '' ) {
            
            // do something...
            $event_object->setHandled( true );
        }
    } );
    $obj->getContents();