Determine calling scope in PHP
Is there a way to determine the scope in which a function is called from inside the function itself? (Or method; I'll use function to mean either in the remainder of开发者_如何学C the post.)
What I do not want is to have to pass __FILE__
or __CLASS__
constants into the function. This information is available in stack traces, etc. so I know it's floating around somewhere, but I'd like to know how to introspect it at runtime without throwing exceptions.
If there are any performance particularities for a solution, I'd like to know about those as well.
There are two use-cases I'm interested in.
- Determine which file / line / function called my function
- Determine the class type that originated the function call
Basically what I want is this:
class util
{
public static function checkScope()
{
echo $magic_scope_stuff;
}
public static function blah()
{
util::checkScope();
}
}
util::checkScope(); // main, line 14
util::blah(); // class util, line 9
There's debug_backtrace()
for that.
The resulting array gives you a call trace from the entry point until the invocation of debug_backtrace
.
精彩评论