FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Function that returns name of itself?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Function that returns name of itself? [message #169451] Mon, 13 September 2010 17:18 Go to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
is there a function that one can use in (for example) an echo or print
command for debugging that will return the name of the function?

For instance

<?php



function abc(){
echo my_name_is();
return 0;
}

function def(){
echo my_name_is();
return 2;
}
<?

I guess I could hardcode the function name in the echo statement, but I
like messing around. :) I did look at php.net for a while but couldn't
find anything.
Re: Function that returns name of itself? [message #169452 is a reply to message #169451] Mon, 13 September 2010 17:33 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
MikeB wrote:
> is there a function that one can use in (for example) an echo or print
> command for debugging that will return the name of the function?
>

name of WHAT function.

The function you are in?

Sort of stack tracer ;-)

I found it more useful to make a little 'errprint' function.

And print a number or a message in a little box at the corner of the
screen, using something like a scrollable div


So

errprint: OK- 1
errprint: done the dirty bit
errprint: OK-2
errprint: OK-3
errprint: Oh shit.

etc.

call it with e.g. errprint('OK-1')
Re: Function that returns name of itself? [message #169454 is a reply to message #169451] Mon, 13 September 2010 18:21 Go to previous messageGo to next message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 13, 1:18 pm, MikeB <mpbr...@gmail.com> wrote:
> is there a function that one can use in (for example) an echo or print
> command for debugging that will return the name of the function?
>
> For instance
>
> <?php
>
> function abc(){
>     echo my_name_is();
>     return 0;
>
> }
>
> function def(){
>    echo my_name_is();
>     return 2;}
>
> <?
>
> I guess I could hardcode the function name in the echo statement, but I
> like messing around. :)  I did look at php.net for a while  but couldn't
> find anything.

Try debug_backtrace(). Or more specifically

function xxx()
{
/* 0 index is top of the stack, which I kinda don't like, when you
* think about pushing and popping on a call stack...
*/
$me = array_shift(debug_backtrace());
echo $me['function'];
}
Re: Function that returns name of itself? [message #169455 is a reply to message #169454] Mon, 13 September 2010 18:23 Go to previous messageGo to next message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 13, 2:21 pm, matt <matthew.leonha...@gmail.com> wrote:
> On Sep 13, 1:18 pm, MikeB <mpbr...@gmail.com> wrote:
>
>
>
>> is there a function that one can use in (for example) an echo or print
>> command for debugging that will return the name of the function?
>
>> For instance
>
>> <?php
>
>> function abc(){
>>     echo my_name_is();
>>     return 0;
>
>> }
>
>> function def(){
>>    echo my_name_is();
>>     return 2;}
>
>> <?
>
>> I guess I could hardcode the function name in the echo statement, but I
>> like messing around. :)  I did look at php.net for a while  but couldn't
>> find anything.
>
> Try debug_backtrace().  Or more specifically
>
> function xxx()
> {
>   /* 0 index is top of the stack, which I kinda don't like, when you
>    * think about pushing and popping on a call stack...
>    */
>   $me = array_shift(debug_backtrace());
>   echo $me['function'];
>
> }
>
>

Doh! I should actually read the manuals for the functions I suggest!
You can also use __FUNCTION__, which is by far much simpler :)
Re: Function that returns name of itself? [message #169458 is a reply to message #169455] Mon, 13 September 2010 21:45 Go to previous message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
matt wrote:

>
> Doh! I should actually read the manuals for the functions I suggest!
> You can also use __FUNCTION__, which is by far much simpler :)

Nice! Thanks. I'll also look into the debug_backtrace, but that's
exactly what I was looking for.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Job opening at RNT Labs Chandigarh
Next Topic: Something I have never seen before
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 15:58:45 GMT 2024

Total time taken to generate the page: 0.02751 seconds