3v4l.org

run code in 300+ PHP versions simultaneously
<?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; } // 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 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
CarCar
Output for 5.2.11 - 5.2.17, 5.3.1 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
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/QvdPD on line 92
Process exited with code 255.
Output for 5.1.1 - 5.1.6, 5.2.0 - 5.2.10, 5.3.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/QvdPD on line 92
Process exited with code 255.
Output for 5.1.0
Fatal error: fatal flex scanner internal error--end of buffer missed in /in/QvdPD on line 116
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/QvdPD on line 92
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected '{' in /in/QvdPD on line 40
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
Parse error: parse error, unexpected '{' in /in/QvdPD on line 40
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/QvdPD on line 40
Process exited with code 255.

preferences:
240.69 ms | 401 KiB | 393 Q