DEV Community

Cover image for Finding Where A Function Is Defined With PHP!
emre.red
emre.red

Posted on

Finding Where A Function Is Defined With PHP!

There is a function or class in PHP and you want to find out which line of the file it is defined in? I have a solution for you!

This is the biggest problem for us as programmers. There is a function and maybe we waste our days trying to find where it is defined but we can't find it. Sometimes we wrote this function, and sometimes the 'ex-programmer' put it in such places on the work we were working on that he hid that function as if he were hiding it in a golden chest. But don't worry, take a breath, we will find your functions in 5 minutes, line by line.

Alt Text

ReflectionFunction

my_func(); // For example, we want to find this function. 

$refFunc = new \ReflectionFunction('my_func');
// Here we have defined ReflectionFunction to get its details. 

print $refFunc->getFileName() . ':' . $refFunc->getStartLine();
// Here, we have printed the file name and the line where our function is located on the screen.
Enter fullscreen mode Exit fullscreen mode

Other things you can do with ReflectionFunction:

getDocComment: Returns in-function comments.
getEndLine Returns the end line of the function.
getExtensionName Returns the extension where the function is located.
getFileName Returns the file containing the function.
getName returns the function name.
getNamespaceName Returns the field name of the function.
getNumberOfParameters Returns the number of variables.
getNumberOfRequiredParameters Returns the number of required variables for the function.
getParameters Returns variables.
getReturnType Returns the result type of the function.
getShortName Returns the short name of the function.
getStartLine Returns the start line of the function.
getStaticVariables Returns constant variables.
hasReturnType Checks whether the function returns a response. isClosure: Checks if it's an anonymous function.
isDeprecated Checks if its use is not recommended.
isUserDefined Checks if the function is a user-defined function.
returnsReference Checks if the function return value is referenced.

You can also use these functions as we used above. For example, when we write getEndLine() where we write getStartLine() in the code, it will return us the end line of the function.

Top comments (0)