获取函数';PHP中的调用程序

获取函数';PHP中的调用程序,php,Php,我知道PHP中的\uuuu文件\uuuu魔术常量将变成当前执行文件的完整路径和文件名。但是有没有一种方法可以为函数的调用文件获取相同的信息?例如: //foo.php: include "bar.php"; call_it(); //bar.php function call_it() { echo "Calling file: ".__CALLING_FILE__; } 它将输出调用文件:…/foo.php 我知道没有\uuu调用\u FILE\uuuu魔法常数,也没有魔法常数来处

我知道PHP中的
\uuuu文件\uuuu
魔术常量将变成当前执行文件的完整路径和文件名。但是有没有一种方法可以为函数的调用文件获取相同的信息?例如:

//foo.php:
include "bar.php";
call_it();

//bar.php
function call_it() {
    echo "Calling file: ".__CALLING_FILE__;
}
它将输出调用文件:…/foo.php


我知道没有
\uuu调用\u FILE\uuuu
魔法常数,也没有魔法常数来处理这个问题,但是有什么方法可以得到这个信息吗?最简单的解决方案是理想的(例如,使用堆栈跟踪将非常粗糙),但最终我只需要它工作。

你应该看看堆栈跟踪来做这些事情。PHP有一个名为

希望能有帮助

根据同样的原理,您可能会发现它很有用,它做同样的事情,但是php自己处理所有信息的格式化和打印

调试回溯()是你的朋友

这就是我们用来转储当前行的完整堆栈跟踪的内容。要根据您的情况进行调整,请忽略
$trace
数组的顶部

class Util_Debug_ContextReader {
    private static function the_trace_entry_to_return() {
        $trace = debug_backtrace();

        for ($i = 0; $i < count($trace); ++$i) {
            if ('debug' == $trace[$i]['function']) {
                if (isset($trace[$i + 1]['class'])) {
                    return array(
                        'class' => $trace[$i + 1]['class'],
                        'line' => $trace[$i]['line'],
                    );
                }

                return array(
                    'file' => $trace[$i]['file'],
                    'line' => $trace[$i]['line'],
                );
            }
        }

        return $trace[0];
    }

    /**
     * @return string
     */
    public function current_module() {
        $trace_entry = self::the_trace_entry_to_return();

        if (isset($trace_entry['class']))
            return 'class '. $trace_entry['class'];
        else
            return 'file '. $trace_entry['file'];

        return 'unknown';
    }

    public function current_line_number() {
        $trace_entry = self::the_trace_entry_to_return();
        if (isset($trace_entry['line'])) return $trace_entry['line'];
        return 'unknown';
    }
}
class Util\u Debug\u ContextReader{
私有静态函数\u trace\u entry\u to\u return(){
$trace=debug_backtrace();
对于($i=0;$i$trace[$i+1]['class'],
'line'=>$trace[$i]['line'],
);
}
返回数组(
'file'=>$trace[$i]['file'],
'line'=>$trace[$i]['line'],
);
}
}
返回$trace[0];
}
/**
*@返回字符串
*/
公共函数当前_模块(){
$trace_entry=self::the_trace_entry_to_return();
如果(isset($trace_entry['class']))
返回'class'。$trace_条目['class'];
其他的
返回'file'。$trace_条目['file'];
返回“未知”;
}
公共功能当前行号(){
$trace_entry=self::the_trace_entry_to_return();
if(isset($trace_entry['line'])返回$trace_entry['line'];
返回“未知”;
}
}

好的,因此我认为PHP堆栈跟踪实用程序比我预期的更友好,而且这似乎是实现我所希望的最干净的方法。我希望这可以在没有回溯的情况下完成
class Util_Debug_ContextReader {
    private static function the_trace_entry_to_return() {
        $trace = debug_backtrace();

        for ($i = 0; $i < count($trace); ++$i) {
            if ('debug' == $trace[$i]['function']) {
                if (isset($trace[$i + 1]['class'])) {
                    return array(
                        'class' => $trace[$i + 1]['class'],
                        'line' => $trace[$i]['line'],
                    );
                }

                return array(
                    'file' => $trace[$i]['file'],
                    'line' => $trace[$i]['line'],
                );
            }
        }

        return $trace[0];
    }

    /**
     * @return string
     */
    public function current_module() {
        $trace_entry = self::the_trace_entry_to_return();

        if (isset($trace_entry['class']))
            return 'class '. $trace_entry['class'];
        else
            return 'file '. $trace_entry['file'];

        return 'unknown';
    }

    public function current_line_number() {
        $trace_entry = self::the_trace_entry_to_return();
        if (isset($trace_entry['line'])) return $trace_entry['line'];
        return 'unknown';
    }
}