Reputation: 3821
How can I find out in which file and line a given function was defined?
Upvotes: 125
Views: 57544
Reputation: 63
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
Upvotes: 2
Reputation: 166843
I like Tom's solution, so I thought I could share a bit more tricks with ReflectionFunction (it should work on every PHP 5):
one-liner to print the filename:
print (new ReflectionFunction("foo"))->getFileName();
Please note that it won't show you the location for internal functions (such as _), but it still can print the API for it as below.
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 ]
}
}
Upvotes: 7
Reputation: 46650
Heres a basic function that will scan your entire project files for 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
$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;
}
?>
Upvotes: 3
Reputation: 57845
You could also do this in PHP itself:
$reflFunc = new ReflectionFunction('function_name');
print $reflFunc->getFileName() . ':' . $reflFunc->getStartLine();
Upvotes: 257
Reputation: 31255
You'll need an IDE that supports "Open Function Declaration" functionality. A good for for php is Eclipse PDT.
To look for the function definition, highlight the function name, hold CTRL + Click on the name.
Upvotes: 0
Reputation: 4160
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:
grep -R "function funName" *
from within the root folder of the project.
Upvotes: 13
Reputation: 4830
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.
There's no code or function to do this though.
Upvotes: 3
Reputation: 115857
I assume that by "described" you mean "defined". For this, you ideally need a decent IDE that can do it.
Upvotes: 2