2

How can I extract all function names from a PHP file?

The file has many functions that look more or less like the following. Is a regular expression the best way to do it?

public function someFunction(

private function anotherOne(
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
silow
  • 4,256
  • 3
  • 21
  • 24

4 Answers4

4

You could find them with a regular expression:

# The Regular Expression for Function Declarations
$functionFinder = '/function[\s\n]+(\S+)[\s\n]*\(/';
# Init an Array to hold the Function Names
$functionArray = array();
# Load the Content of the PHP File
$fileContents = file_get_contents( 'thefilename.php' );

# Apply the Regular Expression to the PHP File Contents
preg_match_all( $functionFinder , $fileContents , $functionArray );

# If we have a Result, Tidy It Up
if( count( $functionArray )>1 ){
  # Grab Element 1, as it has the Matches
  $functionArray = $functionArray[1];
}
Luke Stevenson
  • 10,357
  • 2
  • 26
  • 41
2

If the PHP file is syntactically correct and only contains function only, you can store the result of get_defined_functions into an array (anyway, is return an array).

include_once the PHP file, compare the latest get_defined_functions with the previous array (like array_diff). Whichever results returned by array_diff are the functions define in the PHP file.

If is an object declaration, the above won't work, although you can make use of function get_class_method, but it is not able to return protected and private methods.

OR

ob_start();
highlight_string(file_get_contents('class.php'));
$html = ob_get_contents();
ob_end_clean();
$dom = new DomDocument;
$dom->loadHTML($html);

You can have a list of DOM with a style attribute you can filter with.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ajreal
  • 46,720
  • 11
  • 89
  • 119
  • Nice solution, with the caveat that by `include` ing or `require` ing the file, you will execute it. – Luke Stevenson Jan 07 '11 at 04:10
  • It's an OOP class so it has functions and properties, not just functions. Will this still work? I also don't understand what the array_diff is for? – silow Jan 07 '11 at 04:11
  • @silow: @ajreal's solution will create an Array of Function Names (before the targetted file is involved), and then another Array afterwards. The `array_diff` function allows you to compare those two Arrays, and see what has been added as a result of the targetted file being involved. – Luke Stevenson Jan 07 '11 at 04:20
1

You can write another PHP file to grep for that, or if you have Linux or Mac OS X, you can use something like:

grep -P "function\s+\w" myfile.php
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
1

If your PHP file contains a class which are having methods like above as you mentioned, after including file, you can use get_class_methods function of PHP which requires only the classname and it will return the methods of a mentioned class. For more information, see get_class_methods.

However, if you are sure about classes but don't know the names of it then you can use the first get_declared_classes function to get classes, and then you can use the above mentioned function to the methods of those classes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nik
  • 4,015
  • 3
  • 20
  • 16