@ 2017-03-24T04:49:22Z <?php
function super(... $args)
{
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 2);
if(($object = $trace[1]["object"]) || ($object = $trace[1]["class"]))
{
$method = $trace[1]["function"];
$parent = get_parent_class($object);
if(method_exists($parent, $method))
{
$method = new ReflectionMethod($parent, $method);
return $method->invoke(is_object($object) ? $object : NULL);
}
else
throw new BadMethodCallException("Method " . $parent . "::" . $method ." does not exist.");
}
else
throw new BadFunctionCallException("super() can only be called from a class context.");
}
class HasSuper
{
protected function super(... $args)
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$method = $trace[1]["function"];
$parent = get_parent_class($this);
if(method_exists($parent, $method))
{
$method = new ReflectionMethod($parent, $method);
$method->invoke($this);
}
else
throw new BadMethodCallException("Method " . $callback . " does not exist.");
}
}
class Foo extends HasSuper
{
public function hello()
{
echo("Hello World!" . PHP_EOL);
}
}
class Bar extends Foo
{
public function hello()
{
$this->super();
super();
}
}
$bar = new Bar;
$bar->hello();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 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 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.4 , 8.3.6 Hello World!
Hello World!
Output for 8.3.5 Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Hello World!
Hello World!
preferences:dark mode live preview
214.78 ms | 402 KiB | 200 Q