生产代码中的PHP debug_backtrace获取有关调用方法的信息? [英] PHP debug_backtrace in production code to get information about calling method?

查看:62
本文介绍了生产代码中的PHP debug_backtrace获取有关调用方法的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有充分的理由不将 debug_backtrace 用于确定调用方法的唯一目的.类,名称和参数列表?不用于调试目的.它在函数名称中带有"debug"一词,这让我感到有点脏,但是它适合我需要做的事情(可以在许多地方调用一个函数,需要从另一个系统调用调用方法).它有效,但这仍然是一个坏主意吗?如果是这样,为什么?

解决方案

它确实有点脏,但是正如已经有充分的文献记载,认为并在其他地方被打死,PHP并不是为优雅而设计的系统. /p>

一个令人费解的原因使用debug_backtrace进行应用程序逻辑是,将来某些使用PHP的开发人员可能会决定这只是调试功能,性能无关紧要".

如果您对执行此操作的更好"方式感兴趣,则可以使用 PHP的魔术常数传入调用方法和类名,然后使用 ReflectionMethod 对象提取您需要的任何其他信息.

我用引号括起来更好,因为尽管这样做会更干净,更正确,但是实例化反射对象 的开销可能比使用debug_backtrace函数要大.

Is there a compelling reason to not use debug_backtrace for the sole purpose of determining the calling method's class, name, and parameter list? Not for debugging purposes. It has the word "debug" in the function name, which makes me feel a little dirty to be using it in this way, but it fit the bill for what I needed to do (a single function that can be called from many places and needs to call the calling method from another system). It works, but is this still a bad idea? If so, why?

解决方案

It does feel a little dirty, but as has been well documented, opined, and beaten to death elsewhere, PHP isn't a system designed for elegance.

One highly convoluted reason not to use debug_backtrace for application logic is it's possible some future developer working on PHP could decide "it's just a debug function, performance doesn't matter".

If you're interested in a "better" way of doing this, you could probably use PHP's magic constants to pass in the calling method and class name, and then use a ReflectionMethod object to extract any other information you need.

I put better in quotes because, while this would be cleaner and more correct, the overhead of instantiating a Reflection object may be greater than using the debug_backtrace function.

这篇关于生产代码中的PHP debug_backtrace获取有关调用方法的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆