CALLEE FUNCTION

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

CALLEE FUNCTION

Post 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...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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"
?>
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

Quickly... nicely done...

Post 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!!!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 ;)
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

Thanks...

Post 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...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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() ).
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post 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;
}
}
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

<?php
class foo {
    function bar(){
        echo get_class($this);
    }
}
$fooobj =& new foo();
$fooobj->bar();
?>
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

hey thanks alot
randomblink
Forum Commoner
Posts: 51
Joined: Wed Jan 28, 2004 11:27 am
Location: Tulsa, Oklahoma, just this side of hell...
Contact:

Post 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...
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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";
    }
}
Post Reply