有什么方法可以将PHP魔术常数作为函数默认值吗? [英] Is there any way to carry down a PHP magic constant as a function default?
问题描述
这里的想法是创建一个用于记录和调试目的的方法,不需要将相关的魔术常数传递给该方法。
The idea here is to create a method for logging and debugging purposes, that doesn't require passing said method the associated 'magic constants'.
有效地,我我正在尝试使用如下方法定义来实现这一点:
Effectively, I'm trying to achieve this using a method definition like so:
function Debug($Message,$File=__FILE__,$Line=__LINE__)
{
...
}
我遇到的问题是,如果我在我正在调试的文件之外的文件中定义了上述方法,我最终会得到文件和该方法所定义的文件中的一行,而不是我所定义的方法
The problem I am running in to is that if I define the above method in a file other than the one I am 'debugging', I end up with the file and line from the file the method is defined in, rather than the one I am 'debugging'.
请考虑以下文件集:
Debugging.php
Debugging.php
<?
function Debug($Message,$File=__FILE__,$Line=__LINE__)
{
echo("$File ( $Line ) :: $Message");
}
?>
Testing.php
Testing.php
<?
Debug("Some message");
?>
输出:
Debugging.php ( 1 ) :: Some message
调用消息时发生在第二个文件中-至此应该很清楚,这不是预期的实现。我当然可以在调用时将那些魔术常量传递给'Debug'方法,但我希望尽可能消除不必要的代码。
When the invocation of the message occurred in the second file - which, as should be clear by this point, isn't the intended implementation. I could of course pass the 'Debug' method those magic constants at the time of invocation, but I'm looking to eliminate unnecessary code if possible.
推荐答案
您将像这样使用函数 debug_backtrace
。
function Debug($Message)
{
$backtrace = debug_backtrace();
echo($backtrace[0]['file'] .'(' . $backtrace[0]['line'] . ') :: ' . $Message);
}
这篇关于有什么方法可以将PHP魔术常数作为函数默认值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!