1

Possible Duplicate:
How to find out where a function is defined?

I have a fairly complex OOP PHP project with lots of classes, libraries and helpers to work with.

While debugging, I sometimes find it hard to locate which instance of a specific function is used in when it exists in different files or classes and I can't tell which class or library was included without looking through the entire code to this point.

I was wondering if there is a language construct, a function or even an external tool (e.g. a debugger) that can help me locate exactly in which file and on which line is a specific function located.

E.g. something like:

moditem(25); // How I call the function usually
echo locate_function('moditem');

To display something like:

Function moditem() is located in file: lib/users.php, line 234.

Here's a snippet:

...
return TRUE;
}

function moditem($id){
    $modify = $this->modify($id);
    return $modify;
}
...

My IDE has a way to lead me to the function declaration, but it gets confused when I have 20 files that have a different version of the function with the same name.

Community
  • 1
  • 1
Dzhuneyt
  • 8,437
  • 14
  • 64
  • 118
  • 1
    I use NetBeans IDE, it has an amazing built-in search functionality that can do what you want. – Adi Jul 17 '12 at 13:21
  • get_included_files(); will show your included files – Waygood Jul 17 '12 at 13:21
  • 3
    Are you inferring that you have the same function in many files doing the same thing? If so you have an architectural problem which if fixed will likely fix the side effect you are asking about. – martynthewolf Jul 17 '12 at 13:22
  • 5
    It sounds like your codebase is part of the problem. Why do you have 20 files with the same function? Is this inheritance or sloppy code? – Joe Flynn Jul 17 '12 at 13:22
  • If all else fails, you can always hack with http://php.net/manual/en/function.debug-backtrace.php – biziclop Jul 17 '12 at 13:22
  • 2
    [If your IDE is confused by YOUR code..](http://cdn.memegenerator.net/instances/400x/23473142.jpg) – Adi Jul 17 '12 at 13:27
  • 1
    possible duplicate of [How to find out where a function is defined?](http://stackoverflow.com/questions/2222142/how-to-find-out-where-a-function-is-defined) (and more duplicates like [Get filename and line number of start of function declaration \[closed\]](http://stackoverflow.com/questions/7981905/get-filename-and-line-number-of-start-of-function-declaration)). – hakre Jul 18 '12 at 18:26

2 Answers2

13
$ref = new ReflectionFunction('moditem');
echo $ref->getFileName();

See http://php.net/reflectionfunction and similar related reflection methods.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

Usually in an IDE you can press CTRL + CLICK and it will bring you to the function declaration.

Or if you just want debug output you can do this in the function:

echo __FILE__ . ":" . __LINE__;

Here are the docs on the magic constants

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • 2
    This won't solve the problem though: just show you which LINE and FILE the echo statement is at. If he could place that in his function, he'd probably already know its location. :-) – MatsLindh Jul 17 '12 at 13:28
  • @fiskfisk lol I am saying to put it in **all** versions of the functions. – Naftali Jul 17 '12 at 13:36