使用来自另一个范围的魔术常量

使用来自另一个范围的魔术常量

本文介绍了使用来自另一个范围的魔术常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类 Debug ,其中所有属性和方法都是静态的。使用我使用此类作为(实际上我现在测试性能问题,所以我现在想要什么,什么时候去)。

I created a class Debug in which all properties and methods are static. Using late static binding I use this class as a logger of what is being done and at which moment (in fact I'm testing now performance issue, so I would like to now what and when goes).

所以目前我在每个类的每个main方法中都有一些东西,比如 Debug :: log(__ CLASS__。'::'; __METHOD __); 。在 Debug :: log()方法中,我可以添加时间并将其存储在一些数组中。

So at the moment I have something in each main method of each class like Debug::log(__CLASS__ . '::' . __METHOD__);. In Debug::log() method I can add time and store it in some array.

d想要某一天改变行为我需要改变许多文件中的许多代码...

If I'd wanted some day to change behaviour I would need to change lots of code in many files...

我的问题是:它可能以某种方式省略这些 __ CLASS__。 '::'。 __METHOD __ Debug :: log()方法将知道从哪个类以及从哪个方法调用它?

My question is: is it possible somehow to omit these __CLASS__ . '::' . __METHOD__ and the Debug::log() method would know from which class and from which method it was called?

调用堆栈可能?

推荐答案

您正在寻找的函数是;它会给你一个数据结构,你可以使用这个目的。没有办法让魔术常量以这种方式工作,虽然我同意这个想法,你可以考虑发布一个特性请求,允许在函数定义中用作默认值的魔术常量在调用时被计算,而不是定义时间。我会支持这样的功能请求。

The function you are looking for is debug_backtrace(); it will give you a data structure you can use for this purpose. There is no way to make the magic constants work that way, although I agree with the idea and you might consider posting a feature requests to allow magic constants used as default values in function definitions to be evaluated at call time, not define time. I would support such a feature request.

这篇关于使用来自另一个范围的魔术常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:19