How can I find out in which file and line a given function was defined?
8 Answers
You could also do this in PHP itself:
$reflFunc = new ReflectionFunction('function_name');
print $reflFunc->getFileName() . ':' . $reflFunc->getStartLine();

- 57,217
- 21
- 114
- 142
-
10Tip: `$reflFunc->isInternal() === TRUE` means ->getFileName() and ->getStartLine() will return FALSE. – Bob Stein Jul 05 '13 at 17:11
-
This still works well for me. I'd like to target where the function is being fired, instead of from where it's being defined. – EHerman May 07 '14 at 17:06
-
2@EHerman I don't think you can find callers of a function with reflection. If you could it probably wouldn't work well for this because PHP files tend to be included on demand, and so you would likely not have all the code loaded which does call the function. – Tom Haigh May 08 '14 at 16:22
-
13For Class methods you can use `$reflFunc = new ReflectionMethod($this, 'method_name');` – Ladislav Gallay Aug 24 '14 at 09:12
-
4@EHerman you could look into debug_backtrace() for finding where a function is being called. – dotVezz Sep 05 '14 at 18:01
-
@LadislavGallay Great tip! I think this should be added to the answer. – carla Mar 31 '16 at 20:56
-
Worked for me in modified shop – Naveen Saroye May 20 '23 at 13:33
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.

- 4,037
- 4
- 34
- 43
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 ] } }

- 155,785
- 88
- 678
- 743
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.

- 4,772
- 5
- 29
- 42
-
Same in Zend Studio and I assume this will work with PDT for Eclipse then as well. – Gordon Feb 08 '10 at 15:22
-
@Gordon "an IDE like" I think this is a must have for _any_ modern IDE… – feeela Mar 15 '12 at 14:20
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;
}
?>

- 46,049
- 7
- 62
- 106
I assume that by "described" you mean "defined". For this, you ideally need a decent IDE that can do it.

- 113,561
- 39
- 200
- 288
-
I'm using Aptana which have text search through project. But to use it I need to import the hole (very large) site as a project. – foreline Feb 08 '10 at 14:26
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

- 63
- 5
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.

- 30,349
- 24
- 103
- 144