
Originally Posted by
33degrees
That will only work if there's only a single static call on the line; if there are more than one, it will always return the first one...
Hmm, thanks for pointing that out. How about something like this?
PHP Code:
function get_calling_class() {
$trace = debug_backtrace();
for ($i = 1; $i < count($trace); $i ++) {
$item = $trace[$i];
if (isset($trace[$i + 1]['class']) && get_parent_class($trace[$i + 1]['class']) == $item['class']) {
continue;
} else {
break;
}
}
$lines = file($item['file']);
$line = $lines[$item['line'] -1];
preg_match('@[a-z_][a-z0-9_]*(?=::' . $item['function'] . ')@i', $line, $matches);
return $matches[0];
}
Seems to test with something like:
PHP Code:
class Pappa {
function nameCallingChild() {
echo get_calling_class();
}
}
class SonnyBoy extends Pappa {
function nameCallingGrandChild() {
return parent::nameCallingChild();
}
}
class Grandson extends SonnyBoy {
}
class BigThing {
}
class Thing extends BigThing {
function doThing() {
Grandson::nameCallingGrandChild();
}
function doOtherThing() {
Grandson::nameCallingChild();
}
}
$obj = new Thing();
$obj->doThing();
// echos "Grandson"
$obj->doOtherThing();
// echos "Grandson"
Bookmarks