3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class Observer { static protected $events = array(); static public function register($callable, $event){ if (!isset(self::$events[$event])) { self::$events[$event] = array(); } self::$events[$event][] = $callable; } static public function notify($event, $params = array()) { if (isset(self::$events[$event])) { foreach (self::$events[$event] AS $callable){ call_user_func_array($callable, $params); } } } } function print_meta($p1, $p2) { echo $p1 . ' ' . $p2 . PHP_EOL; } function multiply_meta($p1, $p2) { echo $p1 * $p2 . PHP_EOL; } function add_meta($p1, $p2) { echo $p1 + $p2 . PHP_EOL; } Observer::register('print_meta', 'event1'); Observer::register('multiply_meta', 'event1'); Observer::register('print_meta', 'event2'); Observer::register('add_meta', 'event2'); Observer::register('multiply_meta', 'event3'); Observer::register('add_meta', 'event3'); echo "Notifying of event 1" . PHP_EOL; Observer::notify('event1', array(1, 2)); echo "Notifying of event 2" . PHP_EOL; Observer::notify('event2', array(3, 4)); echo "Notifying of event 3" . PHP_EOL; Observer::notify('event3', array(5, 6));
based on psPpB
Output for 5.0.2 - 7.1.0
Notifying of event 1 1 2 2 Notifying of event 2 3 4 7 Notifying of event 3 30 11
Output for 5.0.0 - 5.0.1
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Ov24F on line 41 Notifying of event 1PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Ov24F on line 21 1 2PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Ov24F on line 25 2PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Ov24F on line 43 Notifying of event 2PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Ov24F on line 21 3 4PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Ov24F on line 29 7PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Ov24F on line 45 Notifying of event 3PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Ov24F on line 25 30PHP_EOL Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/Ov24F on line 29 11PHP_EOL
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STATIC, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Ov24F on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STATIC, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Ov24F on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Ov24F on line 4
Process exited with code 255.