PHP code example of motokraft / event

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

    

motokraft / event example snippets




use \Motokraft\Event\EventHelper;
use \Motokraft\Event\EventMethod;
use \Motokraft\Event\ObjectEvent;
use \Motokraft\Event\EventInterface;
use \Motokraft\Event\EventTypeInterface;
use \Motokraft\Event\Traits\EventTrait;
use \Motokraft\Object\BaseObject;

class UserObject extends BaseObject implements EventTypeInterface
{
    use EventTrait;

    private int $id = 0;
    private string $name = 'admin';
}

EventHelper::addTypeClass('user', UserObject::class);

class DemoEvent implements EventInterface
{
    function onPrepareUserObject(ObjectEvent $event) : void
    {
        $target = $event->getTarget();
        $target->set('login', 'SuperUser');
    }
}

UserObject::addEventMethod('prepare', new EventMethod(
	DemoEvent::class, 'onPrepareUserObject', 1
));

$user = new UserObject;

$event = $user->getObjectEvent('prepare');
$user->dispatchEvent($event);

print_R($user);

$ php composer