Print which class is calling a 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
dhinge
Forum Newbie
Posts: 12
Joined: Mon Jan 08, 2007 1:56 pm

Print which class is calling a function

Post by dhinge »

I want to print the class calling a function in another class, for instance:

class Function_Class {
function callFunction() {
echo "The calling class is X";
}
}
class Calling_Class {
$functionClass = new Function_Class();

$functionClass->callFunction();
}

I want callFunction() to print the class calling it without having to pass $this or something like that. Is this possible?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Print which class is calling a function

Post by requinix »

Yes, but you should have a really good reason for it.

So why do you want to do this?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Print which class is calling a function

Post by superdezign »

I believe that this technique is known as "tracing." However, this is normally done for debugging, and debugging only.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Print which class is calling a function

Post by McInfo »

PHP Manual: debug_backtrace()

Edit: This post was recovered from search engine cache.
Post Reply