是否有可能在php中获得调用方上下文的神奇常量?


Is it possible to get the magic constants of the caller context in php?

我想创建一个调试函数,用于转储有关执行上下文的信息以及其他一些信息。

在debug.php中,我有一个函数,它转储作为参数传递的任何内容。我感兴趣的是从example.php调用转储函数,让它返回文件名,行号和调用上下文的函数名。

1. <?php
2. function my_function(){
3.   $var = 'My example';
4.   dump( $var );
5. }
6. ?>

example.php

我希望上面的函数输出:"example.php, my_function, 4: My example".

是否有这样做的方式(除了传递__FILE__, __FUNCTION____LINE__)作为参数?

可以,您可以使用debug_backtrace()

它以相反的顺序返回所有调用者和文件(跟踪包括文件)的数组(最高祖先最后)。所以调用者应该是数组中的第一个:

$caller_info = next(debug_backtrace());

PHP的debug_backtrace():

print_r(debug_backtrace());

backtrace的输出是一个由关联数组组成的数组,这些数组为您正在查找的常量命名。对于这个示例代码,debug_backtrace()返回的数组如下所示:

Array
(
    [0] => Array
        (
            [file] => /t.php
            [line] => 9
            [function] => abc
            [args] => Array
                (
                    [0] => a
                )
        )
)

我也使用debug_backtrace,我尝试处理不同的场景,这可能看起来太耗时或坏主意,但对我来说(构建我自己的小mvc非常有帮助)。要处理的场景示例:

  • 我的类的__destroy在退出调用者上下文时被自动调用
  • 方法/销毁我的类自动调用,因为被声明为会话处理程序
  • 我的方法/函数是从全局上下文中调用的(不是从其他函数/方法)
  • 等。

所有这些场景都会影响debug_backtrace带来的结果,我的调试函数(定义如下)试图处理这些场景。

每个函数/方法都是这样定义的

function my_func(){if(__DEBUG){dbg(debug_backtrace(),__CLASS__);}

那么当我想要生成最终代码时,我可以运行一个简单的脚本来替换if语句,例如

a="if(__DEBUG){dbg(debug_backtrace(),__CLASS__);}"
b="//replace_what_ever"
dir="/var/www/mydir/"
find $dir -name '*.inc -exec sed -i "s/$a/$b/g" {} ';
find $dir -name '*.php -exec sed -i "s/$a/$b/g" {} ';

至于调试函数,它可以在脚本的最后,让我们说在index.php的最后,但是如果你已经为会话处理程序定义了一个自己的类,那么最后的命令是在该类的析构函数中执行的。

所以在大多数情况下,当出现问题时,调试函数会让您知道。但是,如果出现致命错误,打印由调试函数创建/存储(存储在某个数组中,这里是$dbg_log)的结果的行将无法打印(例如,打印命令放在index.php的末尾)!!对于这种情况,您需要一个手动处理程序(仅用于致命错误)来打印结果数组,我是这样做的:

register_shutdown_function('handleShutdown');
//Catch fatal errors
function handleShutdown() {
    $error = error_get_last();
    global $dbg_log;
    echo "<pre>";
    if(
            $error['type'] === E_COMPILE_ERROR or
            $error['type'] === E_ERROR or
            $error['type'] === E_PARSE or
            $error['type'] === E_CORE_ERROR or
            $error['type'] === E_CORE_WARNING or
            $error['type'] === E_RECOVERABLE_ERROR
    )//we need to catch fatal errors under debug
    {
        //print_r($error); if you like
        $dbg_log[] = " Tip: Start backward, the failing function (line) cannot echo a text put at the end of the function!";
        $last = count($dbg_log)-1;
        for($i=$last;$i>-1;$i--){
            echo "$i: {$dbg_log[$i]} 'n<br>";
        }
    }
}

$dbg_log用于收集所有这些信息,这些信息在全局上下文中定义(见下文)。

现在我的调试函数是这样的:

define('__DEBUG', true); // Easily switch from/to debug mode
$dbg_log = array(); // Keep log of function calling sequence for debuging purposes
 function dbg($tr, $callee_class){
     global $dbg_log;
    // If the file of the caller exists
    if(isset($tr[0]['file'])){
        $caller = $caller=$tr[0]["file"].':'.$tr[0]["line"]; // Then we can get its name and calling line
        // If the caller is a function then it was a callee before so $tr[1] should exists
        if(isset($tr[1])){
            // If the caller is a class method
            if(isset($tr[1]["class"])){
                $caller = $tr[1]["class"]."whenever->".$caller=$tr[1]["function"].':'.$caller;
            }
            // Else
            else{
                $caller = $tr[1]["function"].$caller;
            }
        }
    }
    // Else this is an auto call by php compiler
    else{
        $caller = 'auto';
    }
    // Log the debug info
    $dbg_log[] = 'Started: '.$callee_class.'::'.$tr[0]['function'].' by '.$caller;
}

$tr (trace)数组包含debug_backtrace的结果,因为它们存在于被调用函数中。另一件要提到的事情是,一个调用者在成为调用者之前总是一个被调用者!!

这些是$dbg_log数组的输出(在致命错误或正常退出时)给出的一些真实(对您没有意义,但是所提供的全部信息是显而易见的)结果:

27: Started: Framework'registry::get by App'View'WebWindowOne->__construct:/var/www/MyProject/protected/view/WebWindowOne/WebWindowOne.php:36 
26: Started: App'View'WebWindowOne::__construct by Application->AppStart:/var/www/MyProject/protected/Web_Application.php:240 
25: Started: Application::AppStart by Application->__construct:/var/www/MyProject/protected/Web_Application.php:150 
24: Started: Framework'riskManager::__construct by Application->{closure}:/var/www/MyProject/protected/Web_Application.php:52 

有很多的信息,行号,类等,与非常少的努力(只使用这个function my_func(){if(__DEBUG){dbg(debug_backtrace(),__CLASS__);})为每个函数/方法你定义。