1. Go to this page and download the library: Download fergusinlondon/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/ */
fergusinlondon / events example snippets
use FergusInLondon\Events\Registry;
use FergusInLondon\Events\Handler;
$registry = new Registry();
$registry->registerHandler("user.creation", new Handler(function($user){
$this->container->get('email')->sendWelcome( $user->email_address );
}));
$registry->registerHandler("", new Handler(function($user){
$this->container->get('logger')->info(
sprintf("New user registration: %s (%d)", $user->name, $user->id)
);
}));
class UserController {
public function create() {
// ...
$user->save();
$registry->trigger("user.creation", $user);
}
}
use FergusInLondon\Events\Registry;
use FergusInLondon\Events\Handler;
$registry = new Registry();
// As Handler objects are - by default - initialised with a Callable parameter. This is overridable via subclassing.
$registry->registerHandler("event.demo", new Handler(function(){
echo "See, this is a very simple event handler.\n";
}));
$registry->registerHandler("event.never", new Handler(function(){
echo "This will never run, as we'll clear all handlers first.\n";
}));
// Handlers can be instantiated inline, and access parameters passed in via Registry::trigger()
$userDeleteHandler = new Handler(function($name, $id){
printf("User created: %s (%d)\n", $name, $id);
});
// Handlers also gain the context of the Handler object. Especially useful if you need to subclass and/or access utility methods.
$userCreateHandler = new Handler(function($name, $id){
printf("User created: %s (%d)\n", $name, $id);
printf(
"Handlers have access to the current Handler object too. (i.e %s)",
$this->registryIdentifier
);
});
// Registering instantiated handlers.
$registry->registerHandler("user.create", $userCreateHandler);
$registry->registerHandler("user.delete", $userDeleteHandler);
// The clearHandlers method is capable of
$registry->trigger("event.demo");
$registry->clearHandlers("event.demo");
$registry->trigger("event.demo");
// It's possible to trigger events whilst passing data to the handler
$user = ["name" => "J. Smith", "id" => 101];
$registry->trigger("user.create", $user);
$registry->trigger("user.delete", $user);
// If clearHandlers is called without any parameters, all handlers are cleared
$registry->clearHandlers();
$registry->trigger("event.never");
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.