如何找出功能的定义位置?


109

如何找出给定功能在哪个文件和行中定义?

Answers:


231

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

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

8
提示:$reflFunc->isInternal() === TRUE意味着-> getFileName()和-> getStartLine()将返回FALSE。
鲍勃·斯坦

这对我来说仍然很好。我想定位的是函数被触发的地方,而不是从定义它的地方开始。
EHerman 2014年

2
@EHerman我认为您无法找到具有反射功能的调用方。如果可以的话,这可能不太好用,因为倾向于按需包含PHP文件,因此您可能没有加载所有调用该函数的代码。
汤姆·海格

11
对于类方法,您可以使用$reflFunc = new ReflectionMethod($this, 'method_name');
Ladislav Gallay 2014年

3
@EHerman,您可以调查debug_backtrace()来查找函数的调用位置。
dotVezz 2014年

12

请使用允许这样做的IDE(我建议使用Eclipse PDT),或者如果在Linux上或使用wingrep,则可以始终使用grep。在Linux中,它将类似于:

grep -R "function funName" *

从项目的根文件夹中。


5
最好有:grep -R“ function \ sfunName” *
大卫

3

如果使用NetBeans之类的IDE,则可以CTRL +单击该函数的使用,并假定文件位于您定义的项目文件夹中,它将带您转到定义的位置。

但是,没有代码或函数可以执行此操作。


在Zend Studio中也是如此,我认为这也将与PDT for Eclipse一起使用。
戈登

@戈登“一个类似的IDE”,我认为这是任何现代IDE 都必须具备的……
Feeela 2012年

2

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


我正在使用通过项目进行文本搜索的Aptana。但是要使用它,我需要将孔(非常大)的位置导入为项目。
SaltLake 2010年

2

这是一个基本功能,它将扫描整个项目文件中的特定字符串,并告诉您该文件位于哪个文件中,以及仅使用基本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;
}
?>

2

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


1

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

  • 一线打印文件名:

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

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

  • 打印函数的定义和参数:

    print new ReflectionFunction("foo");

    例:

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

0

您将需要一个支持“开放功能声明”功能的IDE。Eclipse PDT对PHP很有好处。

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.