I have a parent class and a children class. In a parent class I define a function f() and want to print in it name of children class that extended parent class and name of children method that called this function. I do it like that.
abstract class ParentClass {
public function f() {
$backtrace = debug_backtrace();
echo "Class: " . $backtrace[1]['class']) . "\n";
echo "Function: " . $backtrace[1]['function'];
}
}
class ChildrenClass extends ParentClass {
public function some_func() {
$this->f();
}
}
$o = new ChildrenClass;
$o->some_func();
And it correctly outputs me:
Class: ChildrenClass
Function: some_func
The question is: is my solution appropriate or there is a better way to achieve this?
I tried to use __CLASS__
and __FUNCTION__
but it always gives me a classname and function name of the parent class.