3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function call_user_func_fixed() { $args = func_get_args(); $callable = array_shift($args); return call_user_func_array_fixed($callable, $args); } function call_user_func_array_fixed($callable, $args) { $isStaticMethod = false; $expr = '/^([a-z_\x7f-\xff][\w\x7f-\xff]*)::([a-z_\x7f-\xff][\w\x7f-\xff]*)$/i'; // Extract the callable normalized to an array if it looks like a method call if (is_string($callable) && preg_match($expr, $callable, $matches)) { $func = array($matches[1], $matches[2]); } else if (is_array($callable) && count($callable) === 2 && isset($callable[0], $callable[1]) && (is_string($callable[0]) || is_object($callable[0])) && is_string($callable[1])) { $func = $callable; } // If we're not interested in it use the regular mechanism if (!isset($func)) { return call_user_func_array($func, $args); } $backtrace = debug_backtrace(); // passing args here is fraught with complications for backwards compat :-( if ($backtrace[1]['function'] === 'call_user_func_fixed') { $called = 'call_user_func_fixed'; $contextKey = 2; } else { $called = 'call_user_func_array_fixed'; $contextKey = 1; } try { // Get a reference to the target static method if possible switch (true) { case $func[0] === 'self': if (!isset($backtrace[$contextKey]['object'])) { throw new Exception('Use of self:: in an invalid context'); } $method = new ReflectionMethod($backtrace[$contextKey]['class'], $func[1]); $invokeContext = $backtrace[$contextKey]['object']; break; case $func[0] === 'static': if (!isset($backtrace[$contextKey]['object'])) { throw new Exception('Use of static:: in an invalid context'); } $method = new ReflectionMethod($backtrace[$contextKey]['object'], $func[1]); $invokeContext = $backtrace[$contextKey]['object']; break; case $func[0] === 'parent': if (!isset($backtrace[$contextKey]['object'])) { throw new Exception('Use of static:: in an invalid context'); } $method = new ReflectionMethod($backtrace[$contextKey]['object'], $func[1]); if ($method->getDeclaringClass()->getName() === $backtrace[$contextKey]['class']) { $method = $method->getPrototype(); } $invokeContext = $backtrace[$contextKey]['object']; break; case is_object($func[0]): $method = new ReflectionMethod($func[0], $func[1]); $invokeContext = $func[0]; break; default: $method = new ReflectionMethod($func[0], $func[1]); $invokeContext = !empty($backtrace[$contextKey]['object']) ? $backtrace[$contextKey]['object'] : null; break; } var_dump($invokeContext, $args); // Invoke the method with the passed arguments and return the result return $method->invokeArgs($invokeContext, $args); } catch (Exception $e) { trigger_error($called . '() expects parameter 1 to be a valid callback: ' . $e->getMessage(), E_USER_ERROR); return null; } } class Car { public function run() { return call_user_func_fixed(array('self', 'getName')); // should call toyota } private static function getName() { return 'Car'; } } class Toyota extends Car { public static function getName() { return 'Toyota'; } } $car = new Car(); echo $car->run(); //Car instead of Toyota $toyota = new Toyota(); echo $toyota->run(); //Car instead of Toyota
Output for 5.2.11 - 5.2.17, 5.3.1 - 5.6.28, 7.0.0 - 7.1.0
object(Car)#1 (0) { } array(0) { } Fatal error: call_user_func_fixed() expects parameter 1 to be a valid callback: Trying to invoke private method Car::getName() from scope ReflectionMethod in /in/K3a0U on line 94
Process exited with code 255.
Output for hhvm-3.10.0 - 3.13.2
object(Car)#1 (0) { } array(0) { } Carobject(Toyota)#2 (0) { } array(0) { } Car
Output for 5.1.1 - 5.2.10, 5.3.0
object(Car)#1 (0) { } array(0) { } Fatal error: call_user_func_fixed() expects parameter 1 to be a valid callback: Trying to invoke private method Car::getName from scope ReflectionMethod in /in/K3a0U on line 94
Process exited with code 255.
Output for 5.1.0
Fatal error: fatal flex scanner internal error--end of buffer missed in /in/K3a0U on line 118
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Fatal error: call_user_func_fixed() expects parameter 1 to be a valid callback: Use of self:: in an invalid context in /in/K3a0U on line 94
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected '{' in /in/K3a0U on line 40
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected '{' in /in/K3a0U on line 40
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/K3a0U on line 40
Process exited with code 255.