3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class BaseObserver { protected $events = array(); public function register($callable, $event){ if (!isset($this->events[$event])) { $this->events[$event] = array(); } $this->events[$event][] = $callable; } public function notify($event, $params = array()) { if (isset($this->events[$event])) { foreach ($this->events[$event] AS $callable){ call_user_func_array($callable, $params); } } } } function print_meta($p1, $p2) { echo $p1 . ' ' . $p2 . "\r\n"; } function multiply_meta($p1, $p2) { echo $p1 * $p2 . "\r\n"; } function add_meta($p1, $p2) { echo $p1 + $p2 . "\r\n"; } $observer = new BaseObserver(); $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'); $observer->notify('event1', array(1, 2)); $observer->notify('event2', array(3, 4)); $observer->notify('event3', array(5, 6));
based on sMGjG
Output for 5.0.0 - 7.1.0
1 2 2 3 4 7 30 11
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/ffsQ6 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_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/ffsQ6 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/ffsQ6 on line 4
Process exited with code 255.