<?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();
- 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- Hello World!
Hello World!
preferences:
41.88 ms | 408 KiB | 5 Q