3v4l.org

run code in 300+ PHP versions simultaneously
<?php class sM { static private $_methods = array(); static public function addMethod($name, Closure $cb) { self::$_methods[$name] = $cb; } public function __call($name, array $args) { array_unshift($args, $this); return call_user_func_array(self::$_methods[$name], $args); } public function add($a, $b) { return $a + $b; } } sM::addMethod('add'.$base, function($self, $number) use ($base) { return $self->add($number, $base); }); $sM = new sM(); echo $sM->add10(1)."\n"; echo $sM->add100(1)."\n"; foreach(array(100) as $base) { sM::addMethod('add'.$base, function($self, $number) use ($base) { return $self->add($number, $base); }); } echo $sM->add100(1)."\n"; echo $sM->add1000(1)."\n";
Output for 8.0.10 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Warning: Undefined variable $base in /in/5V1EJ on line 25 Warning: Undefined variable $base in /in/5V1EJ on line 25 Warning: Undefined array key "add10" in /in/5V1EJ on line 15 Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, no array or string given in /in/5V1EJ:15 Stack trace: #0 /in/5V1EJ(31): sM->__call('add10', Array) #1 {main} thrown in /in/5V1EJ on line 15
Process exited with code 255.
Output for 8.0.0 - 8.0.9
Warning: Undefined variable $base in /in/5V1EJ on line 25 Warning: Undefined variable $base in /in/5V1EJ on line 25 Warning: Undefined array key "add10" in /in/5V1EJ on line 15 Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, no array or string given in /in/5V1EJ:15 Stack trace: #0 /in/5V1EJ(31): sM->__call('add10', Array) #1 {main} thrown in /in/5V1EJ on line 15
Process exited with code 255.
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: base in /in/5V1EJ on line 25 Notice: Undefined variable: base in /in/5V1EJ on line 25 Notice: Undefined index: add10 in /in/5V1EJ on line 15 Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /in/5V1EJ on line 15 Notice: Undefined index: add100 in /in/5V1EJ on line 15 Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /in/5V1EJ on line 15 101 Notice: Undefined index: add1000 in /in/5V1EJ on line 15 Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /in/5V1EJ on line 15
Output for 7.3.32 - 7.3.33
Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /in/5V1EJ on line 15 Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /in/5V1EJ on line 15 101 Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /in/5V1EJ on line 15

preferences:
170.58 ms | 402 KiB | 188 Q