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! 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 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(). 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! 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
Archived
This topic is now archived and is closed to further replies.