How to determine where a function is defined?

How to find out in which file and line a specific function is defined?

+78
php
Feb 08 '10 at 14:21
source share
8 answers

You can also do this in PHP itself:

$reflFunc = new ReflectionFunction('function_name'); print $reflFunc->getFileName() . ':' . $reflFunc->getStartLine(); 
+166
Feb 08 '10 at 2:58 p.m.
source share

Either use an IDE that allows you to do this (I would recommend Eclipse PDT), or you can always use it if on Linux, or using the wingrep function. On Linux, it will be something like this:

 grep -R "function funName" * 

from the project root folder.

+10
Feb 08 '10 at 14:25
source share

If you are using an IDE, such as Netbeans, you can CTRL + click on the function and it will lead you to where it is defined if the file is in the project folder that you defined.

There is no code or function.

+3
Feb 08 '10 at 14:24
source share

Here is the main function that scans your entire project files for a specific line and tells you which file it is in and which char position starts with using only the base 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; } ?> 
+2
Apr 02 2018-12-12T00:
source share

I assume that by "described" you mean "specific." To do this, you ideally need a decent IDE that can do this.

+1
Feb 08 '10 at 14:22
source share

I like Tom's solution, so I thought I could share a bit more tricks with ReflectionFunction (it should work on every PHP 5)

  • single line file for printing file name:

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

Note that it will not show you the location of internal functions (such as _ ), but it can still print the API for it, as shown below.

  • To print the definition and function parameters:

     print new ReflectionFunction("foo"); 

    Example:

     $ php -r 'print new ReflectionFunction("_");' Function [ <internal:gettext> function _ ] { - Parameters [1] { Parameter #0 [ <required> $msgid ] } } 
+1
Nov 04 '14 at 16:36
source share

another way to check where a function is defined, try to override this function, the PHP error system will just return an error, telling you where the previously defined function is

+1
Mar 30 '15 at 10:43
source share

You will need an IDE that supports the "Open Function Declaration" functionality. Good for php is Eclipse PDT.

To find the definition of a function, highlight the function name, press CTRL + Press the name.

0
Feb 08 2018-10-10
source share



All Articles