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; } function multiply_meta($p1, $p2) { echo $p1 * $p2; } function add_meta($p1, $p2) { echo $p1 + $p2; } 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 \r\n"; Observer::notify('event1', array(1, 2)); echo "Notifying of event 2 \r\n"; Observer::notify('event2', array(3, 4)); echo "Notifying of event 3 \r\n"; Observer::notify('event3', array(5, 6));
Output for 5.0.0 - 7.1.0
Notifying of event 1 1 22Notifying of event 2 3 47Notifying of event 3 3011
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/lQbgs 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/lQbgs 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/lQbgs on line 4
Process exited with code 255.