3v4l.org

run code in 300+ PHP versions simultaneously
<?php function e($n,$p,$c=0){static $a;@krsort($a[$n]);if($c)$a[$n][$p][]=$c;else foreach($a[$n] as$q)foreach($q as$r)$r($p);} function it($m,$p){echo ($p?'✔︎':'✘')." It $m\n"; if(!$p){$GLOBALS['f']=1;}}function done(){if(@$GLOBALS['f'])die(1);} ob_start(); e( 'event', 0, function ($data) { echo "event, $data, priority 0;"; } ); e( 'event', 10, function ($data) { echo "event, $data, priority 10;"; } ); e( 'other_event', -5, function ($data) { echo "other_event, $data, priority -5;"; } ); e( 'other_event', 5, function ($data) { echo "other_event, $data priority 5;"; } ); e('event', 'dataX'); e('other_event', 'dataY'); $output = ob_get_contents(); ob_end_clean(); it( 'calls event listeners in the right order', $output === 'event, dataX, priority 10;event, dataX, priority 0;other_event, dataY priority 5;other_event, dataY, priority -5;' ); class Foo { private $eventDispatcher; function __construct($eventDispatcher) { var_dump($eventDispatcher);die(); $this->eventDispatcher = $eventDispatcher; } function doStuff() { // ... call_user_func($this->eventDispatcher, "my_event", "my data"); } } ob_start(); $foo = new Foo('e'); e("my_event", 5, function($data) { echo "My event was called with '$data'";}); $foo->doStuff(); $output = ob_get_contents();ob_end_clean(); it( 'is perfectly possible to inject the event dispatcher as a dependency into classes.', $output === "My event was called with 'my data'" );
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught TypeError: krsort(): Argument #1 ($array) must be of type array, null given in /in/K8XF6:1 Stack trace: #0 /in/K8XF6(1): krsort(NULL) #1 /in/K8XF6(6): e('event', 0, Object(Closure)) #2 {main} thrown in /in/K8XF6 on line 1
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Fatal error: Uncaught TypeError: krsort(): Argument #1 ($array) must be of type array, null given in /in/K8XF6:1 Stack trace: #0 /in/K8XF6(1): krsort(NULL) #1 /in/K8XF6(11): e('event', 0, Object(Closure)) #2 {main} thrown in /in/K8XF6 on line 1
Process exited with code 255.
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
✔︎ It calls event listeners in the right order string(1) "e"
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/K8XF6 on line 9
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/K8XF6 on line 9
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/K8XF6 on line 9
Process exited with code 255.

preferences:
317.29 ms | 401 KiB | 456 Q