php 如何找出函数的定义位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2222142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:36:12  来源:igfitidea点击:

How to find out where a function is defined?

php

提问by SaltLake

How can I find out in which file and line a given function was defined?

如何找出给定函数是在哪个文件和行中定义的?

回答by Tom Haigh

You could also do this in PHP itself:

您也可以在 PHP 本身中执行此操作:

$reflFunc = new ReflectionFunction('function_name');
print $reflFunc->getFileName() . ':' . $reflFunc->getStartLine();

回答by Johnco

Either use a IDE that allows doing so (I would recomend Eclipse PDT), or you can allways grep it if on Linux, or using wingrep. In Linux it would be something like:

要么使用允许这样做的 IDE(我会推荐 Eclipse PDT),或者如果在 Linux 上或者使用 wingrep,你总是可以 grep 它。在 Linux 中,它将类似于:

grep -R "function funName" *

from within the root folder of the project.

从项目的根文件夹中。

回答by Stephen Melrose

If you use an IDE like Netbeans, you can CTRL+Click the function use and it will take you to where it is defined, assuming the file is within the project folder you defined.

如果您使用像 Netbeans 这样的 IDE,您可以按 CTRL+单击函数使用,它会带您到定义它的位置,假设文件在您定义的项目文件夹中。

There's no code or function to do this though.

但是没有代码或函数可以做到这一点。

回答by Yohanip

another way to check where does the function defined, try to redefine the function, PHP error system will simply returns an error told you where the function previously defined

另一种检查函数定义在哪里的方法,尝试重新定义函数,PHP错误系统将简单地返回一个错误告诉您先前定义的函数在哪里

回答by Lawrence Cherone

Heres a basic function that will scan your entire project filesfor a specific string and tell you which file it is in and which char position it starts at using only basic php. Hope this helps someone...

这是一个基本功能,它将扫描您整个项目文件中的特定字符串,并告诉您它在哪个文件中以及它从哪个字符位置开始,仅使用基本的 php。希望这可以帮助某人...

<?php 
$find="somefunction()";

echo findString('./ProjectFolderOrPath/',$find);

function findString($path,$find){
    $return='';
    ob_start();
    if ($handle = opendir($path)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if(is_dir($path.'/'.$file)){
                    $sub=findString($path.'/'.$file,$find);
                    if(isset($sub)){
                        echo $sub.PHP_EOL;
                    }
                }else{
                    $ext=substr(strtolower($file),-3);
                    if($ext=='php'){
                        $filesource=file_get_contents($path.'/'.$file);
                        $pos = strpos($filesource, $find);
                        if ($pos === false) {
                            continue;
                        } else {
                            echo "The string '$find' was found in the file '$path/$file and exists at position $pos<br />";
                        }
                    }else{
                        continue;
                    }
                }
            }
        }
        closedir($handle);
    }
    $return = ob_get_contents();
    ob_end_clean();
    return $return;
}
?>

回答by Anton Gogolev

I assume that by "described" you mean "defined". For this, you ideally need a decent IDE that can do it.

我认为“描述”是指“定义”。为此,理想情况下,您需要一个可以做到这一点的体面的 IDE。

回答by kenorb

I like Tom's solution, so I thought I could share a bit more tricks with ReflectionFunction(it should work on every PHP 5):

我喜欢 Tom 的解决方案,所以我想我可以与ReflectionFunction分享更多技巧(它应该适用于每个 PHP 5):

  • one-liner to print the filename:

    print (new ReflectionFunction("foo"))->getFileName();
    
  • 单行打印文件名:

    print (new ReflectionFunction("foo"))->getFileName();
    

Please note that it won't show you the location for internalfunctions (such as _), but it still can print the API for it as below.

请注意,它不会向您显示内部函数(例如_)的位置,但它仍然可以为它打印 API,如下所示。

  • to print the definition and parameters of the function:

    print new ReflectionFunction("foo");
    

    Example:

    $ php -r 'print new ReflectionFunction("_");'
    Function [ <internal:gettext> function _ ] {
      - Parameters [1] {
        Parameter #0 [ <required> $msgid ]
      }
    }
    
  • 打印函数的定义和参数:

    print new ReflectionFunction("foo");
    

    例子:

    $ php -r 'print new ReflectionFunction("_");'
    Function [ <internal:gettext> function _ ] {
      - Parameters [1] {
        Parameter #0 [ <required> $msgid ]
      }
    }
    

回答by Yada

You'll need an IDE that supports "Open Function Declaration" functionality. A good for for php is Eclipse PDT.

您需要一个支持“开放函数声明”功能的 IDE。一个对 php 有利的是 Eclipse PDT。

To look for the function definition, highlight the function name, hold CTRL + Click on the name.

要查找函数定义,请突出显示函数名称,按住 CTRL + 单击名称。