Skip to content

Instantly share code, notes, and snippets.

@im4aLL
Last active February 7, 2024 07:52
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 26 You must be signed in to fork a gist
  • Save im4aLL/548c11c56dbc7267a2fe96bda6ed348b to your computer and use it in GitHub Desktop.
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
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
Copy link

nicolasegp commented Jun 17, 2017

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

Cool! Thanks a lot!

@im4aLL
Copy link
Author

im4aLL commented Nov 13, 2017

@shadowng Thanks buddy! @AntoninaSych thanks!

@JeongDongHee
Copy link

good

@ajaysharavat
Copy link

good example

@roverliang
Copy link

very useful example

@Synida
Copy link

Synida commented Mar 3, 2020

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

@nicolasegp
Copy link

@Synida thx I already edited the link 👍

@aJamDonut
Copy link

aJamDonut commented May 16, 2020

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
Copy link
Author

im4aLL commented Sep 2, 2020

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

@fdciabdul
Copy link

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

@im4aLL
Copy link
Author

im4aLL commented Feb 1, 2023

@fdciabdul which parameter you are talking about?

@fdciabdul
Copy link

@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
Copy link
Author

im4aLL commented Feb 5, 2023

@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