Page 1 of 1
CALLEE FUNCTION
Posted: Thu May 13, 2004 11:42 am
by randomblink
Alright...
Somewhere I have read that you can find out the name of function A inside function B which was called by function A... make sense?
example:
Code: Select all
<?php
function callee()
{
echo "The Name of the function that called me is: " . ???
}
function caller()
{
callee();
}
caller();
// Outputs "The Name of the function that called me is: caller"
?>
I do NOT know what to put for the ??? in the callee function, but I am fairly sure I have seen this done in a book... can someone point me in the right direction? I have already been through PHP.net and found NOTHING! Argh! Help if you can... Thanks...
Posted: Thu May 13, 2004 11:45 am
by markl999
Code: Select all
<?php
function callee()
{
$x = debug_backtrace();
$funcname = $x[1]['function'];
echo 'The Name of the function that called me is: '.$funcname;
}
function caller()
{
callee();
}
caller();
// Outputs "The Name of the function that called me is: caller"
?>
Quickly... nicely done...
Posted: Thu May 13, 2004 11:56 am
by randomblink
Oooooooh...
Danka Shane...
Question tho?
I coulda sworn there was a single function that returned the same thing...
This will work GREAT! But is there anychance there is a cleaner way?
I guess I could even create a function to do what I want...
THANKS markl999!!!
Posted: Thu May 13, 2004 11:57 am
by markl999
I'm not aware of any single function (apart from the single function debug_backtrace()) that will do it. But i wait with baited breathe for someone to point one out

Thanks...
Posted: Thu May 13, 2004 12:01 pm
by randomblink
I just went with a mini-approach...
Code: Select all
<?php
function _caller()
{
$x = debug_backtrace();
$funcname = $x[1]['function'];
Return $funcname;
}
function callee()
{
echo 'The Name of the function that called me is: '. _caller() . "<br />";
}
function caller()
{
callee();
}
caller();
?>
Did the same thing... too cool...
Thanks again...
Posted: Thu May 13, 2004 12:04 pm
by markl999
That wouldn't work. It would always say the caller is callee, i.e it will always say the caller is the function that calls _caller() and not the originating function ( caller() ).
Posted: Thu May 13, 2004 12:06 pm
by PrObLeM
is it possible todo the same thing...but instead return the name of a class
Code: Select all
class foo
{
function bar()
{
return $classname;
}
}
Posted: Thu May 13, 2004 12:08 pm
by markl999
Code: Select all
<?php
class foo {
function bar(){
echo get_class($this);
}
}
$fooobj =& new foo();
$fooobj->bar();
?>
Posted: Thu May 13, 2004 12:10 pm
by PrObLeM
hey thanks alot
Posted: Thu May 13, 2004 12:41 pm
by randomblink
Code: Select all
<?php
class test
{
function test()
{
$this->testee();
}
function _caller()
{
$x = debug_backtrace();
$funcname = $x[2]['function'];
Return $funcname;
}
function testee()
{
echo 'The Name of the function that called me is: '. $this->_caller() . "<br />";
}
}
$t = new test();
?>
This does what I was looking for... Don't understand it, but it works so far... I just changed the $funcname = $x[1] to $x[2]... Again, I don't have a clue WHY this works, I am trying to look it up on php.net and it's running slowly for some reason... BUT so far it works...
Posted: Thu May 13, 2004 12:43 pm
by magicrobotmonkey
if i had to guess i would say $x[0] would be the current function, $x[1] the caller $x[2] the callers caller and so on
Posted: Thu May 13, 2004 12:47 pm
by markl999
if i had to guess i would say $x[0] would be the current function, $x[1] the caller $x[2] the callers caller and so on
Correct. As it's generated from a backtrace, will basically contains the call stack.
Posted: Thu May 13, 2004 3:53 pm
by Weirdan
and here is general function to print backtrace:
Code: Select all
function debug_print_backtrace() {
$x = debug_backtrace();
array_shift($x);
foreach($x as $level => $entry) {
if(isset($entry['args'])&&is_array($entry['args']))
foreach($entry['args'] as $key=>$row) {
if(is_array($row)||is_object($row))
$entry['args'][$key] = ' ArrObj( '.serialize($row).' ) ';
}
echo @$level.' => '.@$entry['class'].@$entry['type'].@$entry['function'].'('.@join(',',@$entry['args']).')'.' ['.@$entry['file'].' : '.@$entry['line'].']'."\n";
}
}