Click to See Complete Forum and Search --> : how to determine which object called a function
ja007
04-15-2007, 12:51 PM
Hi,
is it possible in PHP5 to get a name of a class instance or a class which called my function? I've thought of something like this:
function fction()
{
echo "This function was called by" . function_what_i_want();
}
class Class
{
public function call()
{
fction()
}
}
$c = new Class();
$c->call();
and result of this code would be:
This function was called by Class
NogDog
04-15-2007, 04:00 PM
Here's a possibility:
function fction()
{
$bt = debug_backtrace();
if(isset($bt[1]['class']))
{
echo "Function was called from class '{$bt[1]['class']}'.";
}
}
Weedpacket
04-15-2007, 09:44 PM
Or give fction() a parameter and call it as fction(__CLASS__);
NogDog
04-16-2007, 12:41 AM
Or give fction() a parameter and call it as fction(__CLASS__);
I think I feel more comfortable with this idea. The simple solutions are often the best. :)
WDPEjoe
04-24-2007, 05:22 PM
However, you would have to add __CLASS__ to any function/method you want to be able to trace. I've tried this solution in several instances, and it turns out it's tedious to add the __CLASS__ constant to every definition & call. Also, the argument generally doesn't even make sense in the context of the function being called.
Furthermore, if you're using this to control the behavior of the function (i.e. restricting access to the function to a certain list of classes or having it behave differently depending on where it's called from) you can't rely on the input being valid (it would be very easy to call fction("ValidCallingClass", args ...) from any class, valid or not).
In defense of Weedpacket, the __CLASS__ way of doing this is faster. So, it becomes a classic decision of speed vs. security/simplicity.
Weedpacket
04-25-2007, 08:28 AM
and it turns out it's tedious to add the __CLASS__ constant to every definition & call.Only if you've already written them all.Also, the argument generally doesn't even make sense in the context of the function being called.So would NogDog's.you can't rely on the input being validThen in that case, to start with, you can pass the object itself to the function, have all "valid" classes implement a particular interface, and have a type hint on the function declaration to limit the argument to objects that implement that interface. Then if necessary you can use instanceof to look at the object in more specificity.
An object of an invalid class could still create a bogus object of a "valid" class and pass that to fction, of course, but then again it could also create a bogus object of a valid class and have it call fction(). And if there are enough controls surrounding the creation of objects of valid classes to prevent that, then there is enough in place for fction() to check the object it's being passed for validity without simply relying on whether it's of the right class or not.
NogDog's code has the advantage that there is more information available. For classes and objects more information still is accessible through reflection.
Depends entirely on what "fction()" is supposed to do, of course.
PHP Builder
Copyright WebMediaBrands Inc. All Rights Reserved.