确定使用PHP调用函数的位置

时间:2022-11-20 00:04:05

do you guys know how can i determine from what file was a function called inside of that function?

你们知道我怎样才能确定哪个文件是该函数内部调用的函数?

I was thinking of using debug_backtrace .. but that does not look as an elegant way to do it, and they also enumarate other reasons in another question here

我正在考虑使用debug_backtrace ..但这看起来并不是一种优雅的方式,而且他们还在其他问题中提出了其他原因

So what other alternatives are there ?

那么有什么其他选择呢?

thanks a lot

非常感谢

5 个解决方案

#1


From within the function debug_backtrace() is the only way to determine the caller, unless of course you pass that information as parameter.

在函数内部,debug_backtrace()是确定调用者的唯一方法,除非您将该信息作为参数传递。

Note, that you cannot use default values to do this. E.g.:

请注意,您无法使用默认值来执行此操作。例如。:

function f($param1, $param2, $caller=__FUNCTION__) ...

__FUNCTION__ will be evaluated at parse time, so it'll always have the same value. The function in which scope f is declared.

__FUNCTION__将在分析时进行评估,因此它将始终具有相同的值。声明范围f的函数。

#2


I borrowed this code some time ago from somewhere and it works like a charm using debug_backtrace(). Hope you find it useful:

我不久前从某个地方借用了这段代码,它就像使用debug_backtrace()的魅力一样。希望你觉得它有用:

function backtrace(){
    $backtrace = debug_backtrace();

    $output = '';
    foreach ($backtrace as $bt) {
        $args = '';
        foreach ($bt['args'] as $a) {
            if (!empty($args)) {
                $args .= ', ';
            }
            switch (gettype($a)) {
                case 'integer':
                case 'double':
                    $args .= $a;
                    break;
                case 'string':
                    //$a = htmlspecialchars(substr(, 0, 64)).((strlen($a) > 64) ? '...' : '');
                    $args .= "\"$a\"";
                    break;
                case 'array':
                    $args .= 'Array('.count($a).')';
                    break;
                case 'object':
                    $args .= 'Object('.get_class($a).')';
                    break;
                case 'resource':
                    $args .= 'Resource('.strstr($a, '#').')';
                    break;
                case 'boolean':
                    $args .= $a ? 'TRUE' : 'FALSE';
                    break;
                case 'NULL':
                    $args .= 'Null';
                    break;
                default:
                    $args .= 'Unknown';
            }
        }
        $output .= '<br />';
        $output .= '<b>file:</b> '.@$bt['file'].' - line '.@$bt['line'].'<br />';
        $output .= '<b>call:</b> '.@$bt['class'].@$bt['type'].@$bt['function'].'('.$args.')<br />';
    }
    return $output;
}

#3


The responses here by the mickey mouse crew are typical of the online PHP community! Instead of sharing the answer they ask you why you might need it. Never mind the fact that solomongaby asks a valid question and it's a pretty normal feature to have in standard IDE's and more professional languages like Java and Objective-C.

米老鼠工作人员的回复是在线PHP社区的典型代表!他们没有分享答案,而是问你为什么需要它。不要紧,solomongaby提出一个有效的问题,这是一个非常正常的功能,有标准的IDE和更专业的语言,如Java和Objective-C。

Solomongaby, this is the simplest way to get you what you need:

Solomongaby,这是最简单的方式来满足您的需求:

$bt = debug_backtrace();
$end = end($bt);
var_dump($end['class']); //or var_dump($end['file']);

#4


You will not have many options there. The other option (which was posted in the other question) to force the caller to provde it's information with the function call (using PHP magic constants):

你不会有很多选择。另一个选项(在另一个问题中发布)强制调用者通过函数调用提供它的信息(使用PHP魔术常量):

callFunction(1, 2, 3, __FUNCTION__)
callFunction(1, 2, 3, __CLASS__,  __METHOD__)
callFunction(1, 2, 3, $this,  __METHOD__)

  or

$class = new ReflectionClass( __CLASS__ ); 
$method = $class->getMethod( __METHOD__ );
callFunction(1, 2, 3, $method) // $method would be a ReflectionMethod obj

would be a possible alternative. But it's

将是一个可能的替代方案。但它是

  • obfuscating your code
  • 混淆你的代码

  • can be "manipulated" which might cause the code of callFunction to fail and it would be quite difficult to track those errors down.
  • 可以被“操纵”,这可能导致callFunction的代码失败,并且很难跟踪这些错误。

If I were in your place, I would try to avoid it in a function that is used throughout your code. use debug_backtrace (even if it might be 'slow'). Readable code wins over fast code.

如果我在你的位置,我会尝试在整个代码中使用的函数中避免它。使用debug_backtrace(即使它可能是'慢')。可读代码胜过快速代码。

#5


If this is for debugging purposes, you can throw an exception, and print the functions calls stack where you catch it.

如果这是出于调试目的,您可以抛出异常,并在捕获它的位置打印函数调用堆栈。

#1


From within the function debug_backtrace() is the only way to determine the caller, unless of course you pass that information as parameter.

在函数内部,debug_backtrace()是确定调用者的唯一方法,除非您将该信息作为参数传递。

Note, that you cannot use default values to do this. E.g.:

请注意,您无法使用默认值来执行此操作。例如。:

function f($param1, $param2, $caller=__FUNCTION__) ...

__FUNCTION__ will be evaluated at parse time, so it'll always have the same value. The function in which scope f is declared.

__FUNCTION__将在分析时进行评估,因此它将始终具有相同的值。声明范围f的函数。

#2


I borrowed this code some time ago from somewhere and it works like a charm using debug_backtrace(). Hope you find it useful:

我不久前从某个地方借用了这段代码,它就像使用debug_backtrace()的魅力一样。希望你觉得它有用:

function backtrace(){
    $backtrace = debug_backtrace();

    $output = '';
    foreach ($backtrace as $bt) {
        $args = '';
        foreach ($bt['args'] as $a) {
            if (!empty($args)) {
                $args .= ', ';
            }
            switch (gettype($a)) {
                case 'integer':
                case 'double':
                    $args .= $a;
                    break;
                case 'string':
                    //$a = htmlspecialchars(substr(, 0, 64)).((strlen($a) > 64) ? '...' : '');
                    $args .= "\"$a\"";
                    break;
                case 'array':
                    $args .= 'Array('.count($a).')';
                    break;
                case 'object':
                    $args .= 'Object('.get_class($a).')';
                    break;
                case 'resource':
                    $args .= 'Resource('.strstr($a, '#').')';
                    break;
                case 'boolean':
                    $args .= $a ? 'TRUE' : 'FALSE';
                    break;
                case 'NULL':
                    $args .= 'Null';
                    break;
                default:
                    $args .= 'Unknown';
            }
        }
        $output .= '<br />';
        $output .= '<b>file:</b> '.@$bt['file'].' - line '.@$bt['line'].'<br />';
        $output .= '<b>call:</b> '.@$bt['class'].@$bt['type'].@$bt['function'].'('.$args.')<br />';
    }
    return $output;
}

#3


The responses here by the mickey mouse crew are typical of the online PHP community! Instead of sharing the answer they ask you why you might need it. Never mind the fact that solomongaby asks a valid question and it's a pretty normal feature to have in standard IDE's and more professional languages like Java and Objective-C.

米老鼠工作人员的回复是在线PHP社区的典型代表!他们没有分享答案,而是问你为什么需要它。不要紧,solomongaby提出一个有效的问题,这是一个非常正常的功能,有标准的IDE和更专业的语言,如Java和Objective-C。

Solomongaby, this is the simplest way to get you what you need:

Solomongaby,这是最简单的方式来满足您的需求:

$bt = debug_backtrace();
$end = end($bt);
var_dump($end['class']); //or var_dump($end['file']);

#4


You will not have many options there. The other option (which was posted in the other question) to force the caller to provde it's information with the function call (using PHP magic constants):

你不会有很多选择。另一个选项(在另一个问题中发布)强制调用者通过函数调用提供它的信息(使用PHP魔术常量):

callFunction(1, 2, 3, __FUNCTION__)
callFunction(1, 2, 3, __CLASS__,  __METHOD__)
callFunction(1, 2, 3, $this,  __METHOD__)

  or

$class = new ReflectionClass( __CLASS__ ); 
$method = $class->getMethod( __METHOD__ );
callFunction(1, 2, 3, $method) // $method would be a ReflectionMethod obj

would be a possible alternative. But it's

将是一个可能的替代方案。但它是

  • obfuscating your code
  • 混淆你的代码

  • can be "manipulated" which might cause the code of callFunction to fail and it would be quite difficult to track those errors down.
  • 可以被“操纵”,这可能导致callFunction的代码失败,并且很难跟踪这些错误。

If I were in your place, I would try to avoid it in a function that is used throughout your code. use debug_backtrace (even if it might be 'slow'). Readable code wins over fast code.

如果我在你的位置,我会尝试在整个代码中使用的函数中避免它。使用debug_backtrace(即使它可能是'慢')。可读代码胜过快速代码。

#5


If this is for debugging purposes, you can throw an exception, and print the functions calls stack where you catch it.

如果这是出于调试目的,您可以抛出异常,并在捕获它的位置打印函数调用堆栈。