如何找出函数的定义?

我怎样才能找出在哪个文件和行中定义了一个给定的函数?

你也可以在PHP中做到这一点:

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

或者使用允许这样做的IDE(我会推荐Eclipse PDT),或者如果在Linux上或使用wingrep,则可以使用grep。 在Linux中,它会是这样的:

 grep -R "function funName" * 

从该项目的根文件夹中。

如果您使用Netbeans这样的IDE,则可以按CTRL +单击函数use,并将其带到您定义的位置,假定该文件位于您定义的项目文件夹内。

没有代码或function来做到这一点,虽然。

下面是一个基本的function,将扫描您的整个项目文件的特定string,并告诉你它是哪个文件,以及哪个字符位置开始只使用基本的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; } ?> 

我假设你所说的“定义”是指“定义”。 为此,理想情况下需要一个体面的IDE,可以做到这一点。

我喜欢Tom的解决scheme,所以我想我可以与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 ] } } 

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

您将需要一个支持“打开函数声明”function的IDE。 Eclipse PDT是一个很好的select。

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