PHP code example of mhndev / event

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

    

mhndev / event example snippets



Event::bind('order.payed',function($order){
    //do some cool stuf here
});


class MyClass {
    public function __invoke($order) {
        //do something here
    }
}

$myObject = new MyClass;


Event::bind('order.payed', $myObject($order));




// pass $order object as second argument
Event::trigger('order.payed', $order);



User::addObserver('before_update', function($driver){

});

class User
{
    use \mhndev\event\ObservableTrait;
    
    
    function update(array $data)
    {
        $user = $this;
        
        $this->fireEvent('before_update', $this);

        $updatedUser = $this->update($array);

        $driver->fireEvent('after_update', $user, $updatedUser);
    }
}




class User
{
    use \mhndev\event\ObservableTrait;
}


class UserRepository
{


    function update($user_identifier, array $data)
    {
        $user = $this->findByIdentifier($user_identifier);

        $user->buildByOptions($data);

        $user->fireEvent('before_update', $user);

        $updatedUser = $this->update($user);

        $driver->fireEvent('after_update', $user, $updatedUser);

        return $updatedUser;
    }
}