确定 PHP 中的调用范围 [英] 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 the post.)
我不想要的是必须将 __FILE__
或 __CLASS__
常量传递到函数中.这些信息在堆栈跟踪等中可用,所以我知道它在某个地方浮动,但我想知道如何在运行时内省它而不抛出异常.
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.
我对两个用例感兴趣.
- 确定哪个文件/行/函数调用了我的函数
- 确定发起函数调用的类类型
基本上我想要的是这个:
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
推荐答案
结果数组为您提供从入口点到调用 debug_backtrace
的调用跟踪.
The resulting array gives you a call trace from the entry point until the invocation of debug_backtrace
.
这篇关于确定 PHP 中的调用范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!