- debug_backtrace: documentation ( source)
- get_parent_class: documentation ( source)
- method_exists: documentation ( source)
<?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();