回答:
PHP自体でこれを行うこともできます。
$reflFunc = new ReflectionFunction('function_name');
print $reflFunc->getFileName() . ':' . $reflFunc->getStartLine();
$reflFunc = new ReflectionMethod($this, 'method_name');
「記述されている」とは「定義された」という意味だと思います。このためには、理想的には、それを実行できるまともなIDEが必要です。
これは、プロジェクトファイル全体をスキャンして特定の文字列を探し、基本的な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;
}
?>
トムのソリューションが好きなので、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 ]
}
}
$reflFunc->isInternal() === TRUE
-> getFileName()および-> getStartLine()はFALSEを返します。