Function that returns name of itself? [message #169451] |
Mon, 13 September 2010 17:18 |
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 |
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 |
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 |
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 |
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.
|
|
|