Jump to content

OOP: Get a parents parent


br0ken

Recommended Posts

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 

Link to comment
https://forums.phpfreaks.com/topic/176978-oop-get-a-parents-parent/
Share on other sites

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.