br0ken Posted October 8, 2009 Share Posted October 8, 2009 Hi, I have extend a class because I need to override a certain function. The class I am overriding also overrides another class. The function I need to override calls it's parents function when it is complete. I have rewritten the function I need to override but I need to call it's parent function. Basically, there are three classes and from the third class I need to be able to access the first class. Is there anyway a class can get it's parent's parent? I know there is parent::function() but I need something like parent::parent::function() but this doesn't work! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176978-oop-get-a-parents-parent/ Share on other sites More sharing options...
KevinM1 Posted October 8, 2009 Share Posted October 8, 2009 Can you show us your classes? Quote Link to comment https://forums.phpfreaks.com/topic/176978-oop-get-a-parents-parent/#findComment-933136 Share on other sites More sharing options...
nankoweap Posted October 8, 2009 Share Posted October 8, 2009 I know there is parent::function() but I need something like parent::parent::function() but this doesn't work! don't do that. i'm not sure exactly what you're attempting to accomplish in the end, but consider this... chain the calls... for instance, if class3 extends class2 extends class1 and the function you're overriding is someFunc, then class3's someFunc() implementation calls parent::someFunc(). likewise, class2's someFunc implementation calls parent::someFunc(). and lastly, class1's someFunc implementation does whatever it does. Quote Link to comment https://forums.phpfreaks.com/topic/176978-oop-get-a-parents-parent/#findComment-933163 Share on other sites More sharing options...
br0ken Posted October 8, 2009 Author Share Posted October 8, 2009 I'm working with the Magento eCommerce framework. It allows you to customise default features by overriding classes. The actual classes are huge so I've written some example classes to illustrate my problem. <?php class Top { public function go() { echo 'Top <br />'; } } class Middle extends Top { public function go() { echo 'Middle<br />'; parent::go(); } } class Bottom extends Middle { public function go() { // Here I want to call Top's go function while bypassing Middle completely } } I declare a instance of Bottom and inside it's go function I need to call Top's go function. I can't call Middle's go function as the processing inside messes with my work in Bottom. I hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/176978-oop-get-a-parents-parent/#findComment-933204 Share on other sites More sharing options...
mikesta707 Posted October 8, 2009 Share Posted October 8, 2009 try Top::go(). I am not sure if thats how you do it in PHP, but if you were using C++ that would be the way Quote Link to comment https://forums.phpfreaks.com/topic/176978-oop-get-a-parents-parent/#findComment-933217 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.