How to find out where a function is defined?

Php

Php Problem Overview


How can I find out in which file and line a given function was defined?

Php Solutions


Solution 1 - Php

You could also do this in PHP itself:

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

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

I assume that by "described" you mean "defined". For this, you ideally need a decent IDE that can do it.

Solution 5 - Php

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;
}
?>

Solution 6 - Php

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 ]
        }
      }
    

Solution 7 - Php

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

Solution 8 - Php

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionforelineView Question on Stackoverflow
Solution 1 - PhpTom HaighView Answer on Stackoverflow
Solution 2 - PhpJohncoView Answer on Stackoverflow
Solution 3 - PhpStephen MelroseView Answer on Stackoverflow
Solution 4 - PhpAnton GogolevView Answer on Stackoverflow
Solution 5 - PhpLawrence CheroneView Answer on Stackoverflow
Solution 6 - PhpkenorbView Answer on Stackoverflow
Solution 7 - PhpYohanipView Answer on Stackoverflow
Solution 8 - PhpYadaView Answer on Stackoverflow