topherg
topherg

Reputation: 4293

Finding the location of a function programmatically

I am making amendments to a website built by another who used quite literally 1000's of files which are called at any point (i assume it was his attempt at implementing DRY), and I am searching for a specific function. Now usually, I would download the entire project (via FTP), import it into my favorite IDE (netbeans) and perform a search, however I am limited because the only way to access the files is individually through an antiquated hosting control panel that has no search functionality or mass download capability.

So, to try to make my job easier, is there any way to perform a lookup details on a function at runtime, such as its file, and what line its on?

The server that the site is running on has xdebug installed (I know, weird for a production server, but I'm not complaining if it can help).

Upvotes: 1

Views: 92

Answers (1)

Jose Armesto
Jose Armesto

Reputation: 13749

You can use the ReflectionFunction class. With the PHP reflection classes you can get meta information about PHP functions and classes. For your problem, you could do:

$function = new ReflectionFunction('function_name');
// $function->getFileName() returns the filename where is defined.
// $function->getStartLine() returns in which line is defined.

And so on. In the link above you get a list of all the methods available.

Upvotes: 4

Related Questions