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!
<?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...
<?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"
?>
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...
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
<?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();
?>
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() ).
<?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...