uckc Posted October 24, 2009 Share Posted October 24, 2009 Is there any built-in function or any way to check if a class method is called from parent class or child class? Example: class ParentClass { __construct(...){ ... } function ABC(...){ if(function called from child){ echo "called from child"; }else{ echo "called from parent"; } } class ChildClass { __construct(...){ ... } function CBA(...){ ... parent:ABC(...); } Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/178870-iffunction-is-called-from-child-class/ Share on other sites More sharing options...
Mark Baker Posted October 24, 2009 Share Posted October 24, 2009 Nothing specific that I can think of, but debug_backtrace() may provide the information Quote Link to comment https://forums.phpfreaks.com/topic/178870-iffunction-is-called-from-child-class/#findComment-943679 Share on other sites More sharing options...
salathe Posted October 24, 2009 Share Posted October 24, 2009 With PHP 5.3.0 came get_called_class() which might be suitable. <?php class ParentClass { public function test() { if (__CLASS__ != get_called_class()) { echo "Called from child (" . get_called_class() . ")\n"; } else { echo "Called from parent (" . __CLASS__ . ")\n"; } } } class ChildClass extends ParentClass { } class GrandChildClass extends ChildClass { } $parent = new ParentClass; $child = new ChildClass; $grand = new GrandChildClass; $parent->test(); $child->test(); $grand->test(); ?> Called from parent (ParentClass) Called from child (ChildClass) Called from child (GrandChildClass) If not, as Mark said it will probably be a job for debug_backtrace(). Quote Link to comment https://forums.phpfreaks.com/topic/178870-iffunction-is-called-from-child-class/#findComment-943723 Share on other sites More sharing options...
uckc Posted October 25, 2009 Author Share Posted October 25, 2009 Great, thank you Mark and salathe! Quote Link to comment https://forums.phpfreaks.com/topic/178870-iffunction-is-called-from-child-class/#findComment-943853 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.