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']);
}
@nicolasegp

nicolasegp commented Jun 17, 2017

Copy link
Copy Markdown

Great code!
๐Ÿ‘

I made some changes to your code and added the support to parameters.

If you are interested:
https://gist.github.com/nicolasegp/0c6707774b5d8bb8a0edb7426d512d3d

@AntoninaSych

Copy link
Copy Markdown

Cool! Thanks a lot!

@im4aLL

im4aLL commented Nov 13, 2017

Copy link
Copy Markdown
Author

@shadowng Thanks buddy! @AntoninaSych thanks!

@JeongDongHee

Copy link
Copy Markdown

good

@ajaysharavat

Copy link
Copy Markdown

good example

@roverliang

Copy link
Copy Markdown

very useful example

@Synida

Synida commented Mar 3, 2020

Copy link
Copy Markdown

@nicolasegp because of the username change, link is changing too:
https://gist.github.com/nicolasegp/0c6707774b5d8bb8a0edb7426d512d3d

@nicolasegp

Copy link
Copy Markdown

@Synida thx I already edited the link ๐Ÿ‘

@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