How to find method name that called currently ex. method

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
atonyman
Forum Newbie
Posts: 2
Joined: Wed Jan 21, 2009 12:17 pm

How to find method name that called currently ex. method

Post by atonyman »

Ok. So I am trying to figure out how to track down the name of the method that called the currently executing method. I've checked FAQs and RTFM etc. Any help greatly appreciated. FYI: I need this for error reporting in an Adapter Pattern db integration class.

Code: Select all

 
class TestClass {
  function __construct(){
    $this->doSomething();
  }
  function doSomething(){
    // Don't know what to put here but I want the value to be '__construct'
   echo "Name of method that called this method is".$SomebodysAnswerHere;
  }
}
 
Cheers,
atonyman
msurabbott
Forum Commoner
Posts: 33
Joined: Thu Jan 01, 2009 10:18 pm
Location: Chicago, IL, USA

Re: How to find method name that called currently ex. method

Post by msurabbott »

I wish I had a response for you but I have actually been looking for this myself for about 2 years..
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: How to find method name that called currently ex. method

Post by Burrito »

I don't think it's available in php like it is in perl (caller.)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: How to find method name that called currently ex. method

Post by pickle »

debug_backtrace()

I use it for pretty much this exact purpose for my dump_array() function. Where the function references ['file'] and ['line'], you can also reference ['function']
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How to find method name that called currently ex. method

Post by VladSun »

Awesome, pickle :)

I also found the __METHOD__ magic constant (PHP 5.0+) :)
In fact it will contain both class name and method called in this format:
class::method

More interesting PHP magics:
http://php.net/manual/en/language.const ... efined.php
There are 10 types of people in this world, those who understand binary and those who don't
atonyman
Forum Newbie
Posts: 2
Joined: Wed Jan 21, 2009 12:17 pm

Re: How to find method name that called currently ex. method

Post by atonyman »

Thanks to everyone's quick replies. :D I have implemented the debug_backtrace for my solution. Since I only wanted the 'calling' class/method I used the second element of the debug_backtrace array (actually [1]).
Here's what my solution looks like in sample code:

Code: Select all

 
  class Test {
        function __construct(){
                $this->doSomething();
        }
        function doSomething(){
                $dbt = debug_backtrace();
                echo "Here is the name of the object and method that called this method :".$dbt[1]['class']." ".$dbt[1]['function'];
        }
}
$test = new Test;
 
The above outputs the following: Here is the name of the object and method that called this method :Test __construct
Post Reply