How to find out where a function is defined using PHP ?

When we do projects then it includes multiple modules where each module divided into multiple files and each file containing many lines of code. So when we declare a function somewhere in the files and forget that what the function was doing or want to change the code of that function but can’t find that where the function is! So this article will help you to find the location of the function.

In order to get the location of the function in PHP we can use the inbuilt class ReflectionFunction(). When the name of the function regarding which we are requiring details is passed to the class constructor, it gets several details regarding that function.

  • getFileName: It returns the file location of the function.
  • getNumberOfParameters: It returns the number of parameters passed to the function.
  • getStartLine: It return the starting line of the function.

Syntax:



$details = new ReflectionFunction('function_name');

Then use the above functions to access whatever you need. Paste the below code into main code and you will get the details of that function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
$details = new ReflectionFunction('printing');
print $details->getFileName() . ':' . $details->getStartLine();
?>

chevron_right


Steps to run the code:

  • Create a file named as file_name.php
  • Copy the above given code into the file.
  • Save the file into your localhost server folder. In the case of WampServer the look for the folder ‘www’ inside ‘wamp64’ folder in C drive and save the file there.
  • Run your wamp server.
  • Open any browser and type localhost/fun.php to get the following output.

Output:

C:\wamp64\www\file_name.php : 2 

Below examples illustrates the ReflectionFunction in PHP:

Example 1: Suppose in the given code we want to find the location of the function ‘printing’. In the output, the filename and location of the function printing can be seen.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
function printing()
{
    echo 'Welcome to GeeksforGeeks';
}
  
$details = new ReflectionFunction('printing');
echo 'File location : '.$details->getFileName(). 
', Starting line : ' . $details->getStartLine().
', Parametrs passed : '.$details->getNumberOfParameters();
?>

chevron_right


Output:

File location : /home/7de5f19b219d214c719df5f3839a7f61.php, 
Starting line : 2, Parametrs passed : 0

Example 2: Suppose in the given code we want to find the location of the function ‘geeks’. In the output, the filename, location, start line and the parameter passed of the function geeks can be seen.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
function printing() {
    echo 'Welcome to GeeksforGeeks';
}
  
function geeks() {
    echo 'This is the article How to find out where 
         a function is defined using PHP?';
}
  
$details = new ReflectionFunction('geeks');
  
print 'File location :'.$details->getFileName().
' Starting line :' . $details->getStartLine().
' No. of parametrs passed :'.$details->getNumberOfParameters();
  
?>

chevron_right


Output:

File location :/home/dd96d70bdf5ff03fea0ea24110bae9ff.php 
Starting line :7 No. of parametrs passed :0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.