Is it possible to tell where a method was called from?

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Is it possible to tell where a method was called from?

Post by Luke »

Say I have one class like this... Is it possible to find out from which class (and method if possible) another class is called from?

Code: Select all

class someClass{
    function someClass(){
        $obj = new otherClass();
        $obj->doSomething();
    }
}
class otherClass{
    function doSomething(){
        $this->source = //Some way to find out where this was called from
    }
}
I know I could pass the doSomething method a variable with $this in it... is there any way for the method to know AUTOMATICALLY though?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

automatically, no. But with debug_backtrace() yes.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Would magic constants work?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

no
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Unless I'm forgetting one, all the magic constants are specific to the lines, files, functions and classes that request them only.
Post Reply