3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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";
based on Sc224
Output for 5.6.30, 7.0.30 - 7.3.0alpha1
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 hhvm-3.22.0
Notice: Undefined variable: base in /in/5V1EJ on line -1 Notice: Undefined variable: base in /in/5V1EJ on line -1 Notice: Undefined index: add10 in /in/5V1EJ on line -1 Warning: Invalid argument: function: not string, closure, or array in /in/5V1EJ on line -1 Notice: Undefined index: add100 in /in/5V1EJ on line -1 Warning: Invalid argument: function: not string, closure, or array in /in/5V1EJ on line -1 101 Notice: Undefined index: add1000 in /in/5V1EJ on line -1 Warning: Invalid argument: function: not string, closure, or array in /in/5V1EJ on line -1
Output for hhvm-3.18.5 - 3.21.3
Notice: Undefined variable: base in /in/5V1EJ on line 25 Notice: Undefined variable: base in /in/5V1EJ on line 27 Notice: Undefined index: add10 in /in/5V1EJ on line 15 Warning: Invalid argument: function: not string, closure, or array in /in/5V1EJ on line 15 Notice: Undefined index: add100 in /in/5V1EJ on line 15 Warning: Invalid argument: function: not string, closure, or array in /in/5V1EJ on line 15 101 Notice: Undefined index: add1000 in /in/5V1EJ on line 15 Warning: Invalid argument: function: not string, closure, or array in /in/5V1EJ on line 15