watagal Posted May 6, 2008 Share Posted May 6, 2008 GreetingsHow do I obtain the function name from within the function? Pseudo code:function getSomething() { echo this.function_name;}TIA,Gal Link to comment Share on other sites More sharing options...
boen_robot Posted May 6, 2008 Share Posted May 6, 2008 Why would you want to do THAT? The name of a function is always known in advance, so why don't you simply function getSomething() {echo 'getSomething';} ?If there's something more to it, at worse you can add a variable at the top of the function, and use that later in the function, like: function getSomething() {$functionName = 'getSomething';echo $functionName;} Link to comment Share on other sites More sharing options...
watagal Posted May 6, 2008 Author Share Posted May 6, 2008 Thanks for the quick response, but I won't know the function name at run time.Case: I have a many functions ("1","2","3") calling one function "A". Within function "A", I would like to determine the calling function ("1","2","3"). Clear as mud?TIA, Gal Link to comment Share on other sites More sharing options...
boen_robot Posted May 6, 2008 Share Posted May 6, 2008 Oh. That's a completely different thing.If the function is within a class, you can adjust a property before calling the function to show the name of the last function called, or... I don't know. Whatever the scenario, you can probably do this without actually finding the name of the caller method.You could also pass an argument to the function made explicitly for the purpose. For example: function getSomething($caller) {echo $caller;}function callGetSomething() {getSomething('callGetSomething');} Link to comment Share on other sites More sharing options...
alexnofue Posted May 6, 2008 Share Posted May 6, 2008 I would do something like keeping record where i am with a session variable, like this:$_SESSION['last_visited'] = 'function_name';not as pretty as trying to pick it with some PHP variable or something like that, but still works. Link to comment Share on other sites More sharing options...
jeffman Posted May 6, 2008 Share Posted May 6, 2008 If by any chance you're debugging, you can always drop in a lot of temporary echo statements to keep tabs on what's what before things break. Probably you know that. Link to comment Share on other sites More sharing options...
Wander Posted May 6, 2008 Share Posted May 6, 2008 isnt what u mean just the __METHOD__ constant? function something(){ echo "function name: ".__METHOD__;}something(); will show:function name: something Link to comment Share on other sites More sharing options...
sooty2006 Posted May 8, 2008 Share Posted May 8, 2008 your not making it clear to as what it is you need!i think this may help tho? <?phpfunction getSomething($call1,$call2,$call3) {if ($call1 == "eg") {echo "$call2";}}?> ??? Link to comment Share on other sites More sharing options...
watagal Posted May 8, 2008 Author Share Posted May 8, 2008 isnt what u mean just the __METHOD__ constant?Thanks, so much. - this is exactly what I was looking for.Gal Link to comment Share on other sites More sharing options...
Wander Posted May 8, 2008 Share Posted May 8, 2008 glad it is, you can also use the __FUNCTION__ one, the difference is that __FUNCTION__ always just shows the function name, __METHOD__ will show it as classname::functionname if the function is i a class Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.