如何在 PHP 中获取调用函数/方法的名称?
我知道函数 debug_backtrace
,但我正在寻找一些现成的函数实现,例如 GetCallingMethodName()
?如果它也给出方法的类(如果它确实是一个方法),那就完美了。
I am aware of function debug_backtrace
, but I am looking for some ready to use implementation of function like GetCallingMethodName()
? It would be perfect if it gave method's class too (if it is indeed a method).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
最简单的方法是:
如下面的评论所述,可以通过将参数传递给以下方式进一步优化:
object
和args
索引The simplest way is:
As noted in the comments below, this can be further optimized by passing arguments to:
object
andargs
indicesdebug_backtrace()
函数是了解这一点的唯一方法,如果您很懒,这也是您应该自己编写GetCallingMethodName()
代码的又一个原因。 与懒惰作斗争! :DThe
debug_backtrace()
function is the only way to know this, if you're lazy it's one more reason you should code theGetCallingMethodName()
yourself. Fight the laziness! :D从 php 5.4 开始,您可以使用
这不会浪费内存,因为它会忽略参数并仅返回最后 2 个回溯堆栈条目,并且不会像此处的其他答案那样生成通知。
As of php 5.4 you can use
This will not waste memory as it ignores arguments and returns only the last 2 backtrace stack entries, and will not generate notices as other answers here.
您还可以使用 php 异常提供的信息,这是一个优雅的解决方案:
您会得到这个...(瞧!)
You can also use the info provided by a php exception, it's an elegant solution:
And you get this... (voilà!)
对我来说,
debug_backtrace
已经达到了内存限制,我想在生产中使用它来记录和发送发生的错误。相反,我发现这个解决方案非常有效!
For me
debug_backtrace
was hitting my memory limit, and I wanted to use this in production to log and email errors as they happen.Instead I found this solution which works brilliantly!
我最喜欢的方式,一行!
您可以这样使用它:
请注意,这仅与去年发布的 PHP 版本兼容。但出于安全原因,无论如何保持 PHP 最新是个好主意。
My favourite way, in one line!
You can use it like this:
Note that this is only compatible with versions of PHP released within the last year. But it's a good idea to keep your PHP up to date anyway for security reasons.
我刚刚写了一个名为“get_caller”的版本,希望它有所帮助。我的很懒。您可以只从函数中运行 get_caller() ,而不必像这样指定它:
这是带有奇怪测试用例的完整脚本:
man() 调用 Woman(),man() 调用 get_caller()。 get_caller()还不知道是谁调用了它,因为woman()很谨慎,没有告诉它,所以它递归地找出来。然后它返回谁调用了woman()。浏览器中源代码模式的打印输出显示了函数堆栈:
I just wrote a version of this called "get_caller", I hope it helps. Mine is pretty lazy. You can just run get_caller() from a function, you don't have to specify it like this:
Here's the script in full with a quirky test case:
man() calls woman(), who calls get_caller(). get_caller() doesn't know who called it yet, because the woman() was cautious and didn't tell it, so it recurses to find out. Then it returns who called woman(). And the printout in source-code mode in a browser shows the function stack:
我需要一些东西来列出调用类/方法(在 Magento 项目上工作)。
虽然
debug_backtrace
提供了大量有用的信息,但它为 Magento 安装喷出的信息量是巨大的(超过 82,000 行!),因为我只关心调用函数和 课堂上,我提出了这个小解决方案:I needed something to just list the calling classes/methods (working on a Magento project).
While
debug_backtrace
provides tons of useful information, the amount of information it spewed out for the Magento installation was overwhelming (over 82,000 lines!) Since I was only concerned with the calling function and class, I worked this little solution up:获取父函数名称的最简单方法是:
The simplest way of getting parent function name is:
我见过的这个问题的最佳答案是:
简短而干净
Best answer of that question I've seen is:
Short and clean