Looking for a clean way to determine the class (in this case, either parent or child class) of the method that calls a method in the parent class.
I thought late static binding could handle this, but seems like that only really works for calling a static method directly, and not from within an instantiated object's method.
Consider the following:
abstract class ParentClass { public function parentMethod() { self::_log("parent.non.static"); } public static function parentStatic() { self::_log("parent.static"); } public static function getClassName() { return __CLASS__; } protected static function _log($key) { $prefix = 'graphite.key.prefix'; $class = static::getClassName(); // gets the object's class, not calling class $g_key = "{$prefix}.{$class}.{$key}"; echo "{$g_key} \n"; // Graphite::increment($g_key); } } class ChildClass extends ParentClass { public function childMethod() { self::_log("child.non.static"); } public static function childStatic() { self::_log("child.static"); } public static function getClassName() { return __CLASS__; } } $obj = new ChildClass; $obj->childMethod(); // graphite.key.prefix.ChildClass.child.non.static $obj->parentMethod(); // graphite.key.prefix.ChildClass.parent.non.static ParentClass::parentStatic(); // graphite.key.prefix.ParentClass.parent.static ChildClass::childStatic(); // graphite.key.prefix.ChildClass.child.static Looking for a clean way to get the class that calls the _log() method without having to pass it in as a parameter. Doesn't have to be static at all, but I was playing around with the late static binding, because I thought that would work, but it just gets the name of the instantiated object, not the child/parent class of the method that calls the _log() method :-/
Edit:
Just to be clear, I'm after getting the class name of the method that called _log() from within the instantiated object (like parentMethod() and childMethod()) Don't care if _log() is static or not. If that makes it easier, fine. But the static ParentClass::parentStatic() and ChildClass::childStatic() were just to show late static bindings and what I figured might work, but not from calling within an instantiated object
get_class($this)maybe?$thisin a static method.$instance->childMethod()that callsparent::parentMethod1()that callsself::parentMethod2(). InparentMethod2you want to show that it was called byparentMethod1(or really just parent). If that is the case, the only method I know of would be to parsedebug_backtrace(). Although, it isn't that bad considering the calling function/class would always be at key1.