Skip to content

Instantly share code, notes, and snippets.

@im4aLL
Last active December 1, 2025 00:01
Show Gist options
  • Select an option

  • Save im4aLL/548c11c56dbc7267a2fe96bda6ed348b to your computer and use it in GitHub Desktop.

Select an option

Save im4aLL/548c11c56dbc7267a2fe96bda6ed348b to your computer and use it in GitHub Desktop.
PHP event listener simple example
<?php
// Used in https://github.com/im4aLL/roolith-event
// https://packagist.org/packages/roolith/event
class Event {
private static $events = [];
public static function listen($name, $callback) {
self::$events[$name][] = $callback;
}
public static function trigger($name, $argument = null) {
foreach (self::$events[$name] as $event => $callback) {
if($argument && is_array($argument)) {
call_user_func_array($callback, $argument);
}
elseif ($argument && !is_array($argument)) {
call_user_func($callback, $argument);
}
else {
call_user_func($callback);
}
}
}
}
class User {
public function login() {
return true;
}
public function logout() {
return true;
}
public function updated() {
return true;
}
}
// Usage
// ==================================
Event::listen('login', function(){
echo 'Event user login fired! <br>';
});
$user = new User();
if($user->login()) {
Event::trigger('login');
}
// Usage with param
// ==================================
Event::listen('logout', function($param){
echo 'Event '. $param .' logout fired! <br>';
});
if($user->logout()) {
Event::trigger('logout', 'user');
}
// Usage with param as array
// ==================================
Event::listen('updated', function($param1, $param2){
echo 'Event ('. $param1 .', '. $param2 .') updated fired! <br>';
});
if($user->updated()) {
Event::trigger('updated', ['param1', 'param2']);
}
@aJamDonut

aJamDonut commented May 16, 2020

Copy link
Copy Markdown

Working on parsing my files to document events. Regex below, taking suggestions on anything missing or alternative regexes thanks.

Gist

preg_match('/Event::trigger\(((\'|\").*)(,|\',) \[(.*)]\)/', $line, $matches);

@im4aLL

im4aLL commented Sep 2, 2020

Copy link
Copy Markdown
Author

Package published https://github.com/im4aLL/roolith-event if anyone wants to use it.

@fdciabdul

Copy link
Copy Markdown

it doesn't want to pass an array to parameters , why ?

@im4aLL

im4aLL commented Feb 1, 2023

Copy link
Copy Markdown
Author

@fdciabdul which parameter you are talking about?

@fdciabdul

Copy link
Copy Markdown

@fdciabdul which parameter you are talking about?

i mean like this

Event::listen('logout', function($param){
    echo 'Event '. $param .' logout fired! <br>';
});

if($user->logout()) {
$data = array("first","second","third");


    Event::trigger('logout', $data);
}

the output is just first , not return the array itself

@im4aLL

im4aLL commented Feb 5, 2023

Copy link
Copy Markdown
Author

@fdciabdul you can call multiple params function($param1, $param2, $param3) or if you want the whole array then just call it like this -

Event::trigger('logout', [$data]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment