1

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

I wanna know (programmatically, probably through reflection API) where a certain PHP function is defined.

Community
  • 1
  • 1
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68
  • 1
    What do you mean by *where* ? You mean in which file or which class or what ? – Sarfraz Mar 10 '12 at 09:51
  • @Sarfraz I was looking for file:line but how do you give a method name and find out the classes having such method ? – Ashkan Kh. Nazary Mar 10 '12 at 09:59
  • @ashy_32bit: please update the question with the requested information. In general, respond to requests for clarifications by updating your post, rather than replying with a comment. For one thing, a question should be understandable without reading comments. For another, SO is a QA & site, not a forum, and comments aren't intended (nor are they well suited) for discussions. – outis Mar 10 '12 at 15:17
  • @outis That was the firs thing I was going to do when I first read Sarfraz's comment but then I realized the question is clear and the ambiguity arises from the so called comment and not the question in itself (as you can see there are correct answers) so I decided to leave a comment and clarify. – Ashkan Kh. Nazary Mar 11 '12 at 12:16
  • @ashy_32bit: on the contrary, Sarfraz's comment is a request for clarification. It can't introduce ambiguity, as it adds no information. The question is not clear. When a question isn't clear, you may still get correct answers, as people can always guess or make assumptions at what you mean. However, guesses and assumptions lead to bugs. It's essential to practice clarity of thought and communication in technical matters. – outis Mar 11 '12 at 22:34
  • @outis I must disagree I'm afraid, as one may ask for clarification even in the most obvious of situations due to one's lack of knowledge about the subject matter whereas an expert would be clear about what exactly is being asked (P.S: not that Sarfraz's comment is such). – Ashkan Kh. Nazary Mar 12 '12 at 07:20
  • @ashy_32bit: if Sarfraz's comment doesn't arise from lack of expert knowledge, then how is the scenario you describe relevant? A request for clarification arises because a question (or statement) is underspecified; in the situation you describe, the missing information is common knowledge to other discoursers (here, experienced developers), and can thus safely be left out. However, that's not what we're dealing with here. Knowing what you mean by "where" isn't common knowledge to anyone but you. – outis Mar 12 '12 at 07:52
  • @outis Very well said but this is not a matter of *exactness*, rather a matter of (ambiguous?) *consciousness*. We are not doing a compiler design here, nor are we writing some specifications. Sure there are myriads ways to interpret the "where" here but a very sane and sound assumption could be made (that I'm trying to find the line:number). Everyone else made that assumption, answered my question, I got what I was looking for, and done away with the whole thing *before* the so called comment was even posted. – Ashkan Kh. Nazary Mar 12 '12 at 09:31
  • @ashy_32bit: consciousness can't be ambiguous, but someone can be uncertain about meaning. Now we have to return to my original point: clarifications should be edited into the question rather than made as comments. Your original reason for not doing so doesn't hold water. Even if you've gotten the answer you seek, it's of benefit to others looking for answers. For one, someone who was looking for a different "where" wouldn't waste time reading this question. Perhaps most importantly, it impacts searches. A searcher would probably include the terms "file" and "line" in the search. – outis Mar 13 '12 at 20:56
  • ... SO's local search may (now or in the future) give greater weight to content in the question than in comments. Even external search engines, which won't take into account SO's particular format, may weight words based on how closely they appear in the text. By having some information in a comment, it can negatively impact this question's rank. – outis Mar 13 '12 at 20:58
  • @outis Now we are making it a forum you and I ;-) My terrible mistake: I intended to say "conciseness" and not "consciousness", courtesy of my hyper-smart spell checker. I meant to say the question is short and reasonably clear within bounds. Now you can question even the clearest of things (as happens all the time) but you wouldn't over-verbalize it to satisfy everyone. I believe inserting "file:line" in the question would be unnecessary and cumbersome as it is kinda obvious what is being asked (not to everyone for sure, but that doesn't mean question needs revision). – Ashkan Kh. Nazary Mar 15 '12 at 07:33
  • @ashy_32bit: that makes more sense. It's a fair point. As a counterpoint, precision and accuracy are a part of being concise. Adding "file and line" actually would improve the conciseness of the question. "Obvious" is just another word for "assumption", and assumptions will wind up biting you on the ass. However, we've abused the comment system long enough, and have probably reached as much consensus as we're going to reach. – outis Mar 16 '12 at 01:28

4 Answers4

5

There's stuff in the ReflectionFunction class that looks relevant (getFileName, getStartLine, etc.).

(Untested)

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

debug_backtrace() will give back an array with all calls being made, and also the definition of the functions/methods. The reflection class will give you the definition of the function.

e.g. I use this to log deprectated function within older projects:

function logDeprecated() {
    $trail = debug_backtrace();
    if ($trail[1]['type']) {
        $function = new ReflectionMethod($trail[1]['class'], $trail[1]['function']);
    } else {
        $function = new ReflectionFunction($trail[1]['function']);
    }
    $errorMsg = 'Function ' . $trail[1]['function'];
    if ($trail[1]['class']) {
        $errorMsg .= ' of class ' . $trail[1]['class'];
    }
    $errorMsg .= ' is deprecated (called from ' . $trail[1]['file'] . '#' . $trail[1]['line'] . ', defined in ' . $function->getFileName() . '#' . $function->getStartLine() . ')';
        return $errorMsg;
}
konsolenfreddy
  • 9,551
  • 1
  • 25
  • 36
  • perhaps I didn't get it well enough but does it actually tell you anything about where the trace elements are *defined* rather than called ? – Ashkan Kh. Nazary Mar 10 '12 at 09:57
  • @ashy_32bit: You are right, it doesn't really show where it's defined. I've added an example how I log deprecated functions using Reflection – konsolenfreddy Mar 10 '12 at 10:03
1
ReflectionMethod::getStartLine()
ReflectionMethod::getFileName()
ReflectionFunction::getStartLine()
ReflectionFunction::getFileName()
Ashkan Kh. Nazary
  • 21,844
  • 13
  • 44
  • 68
0

probably it is not best solution, but in case you wont find anyting, you can find function location using grep command if you are using linux.

exec('grep -irn "yourFunctionName" ./library', $response);
foreach ($response as $file) {
    list($filename, $line) = explode(":", $file);
}
b.b3rn4rd
  • 8,494
  • 2
  • 45
  • 57