tomcatexodus
Aug 28th, 2010, 05:35 AM
I know many of my threads deal with optimization, but I'm trying to broaden my knowledge base of common bottlenecks.
Anyhow, as of currently an application I'm working on makes use of a couple classes I've created.
For simplicity:
abstract class WidgetBase{}
class AlphaWidget extends WidgetBase{}
class BetaWidget extends WidgetBase{}
class GammaWidget extends WidgetBase{}
Now, all is fine and good, my abstract WidgetBase has a nice collection of inherited methods/properties and everything is clean. The issue is though, that GammaWidget is complicating things, as it has an additional property of instance (or index) that adds an additional dimension to the array properties it inherits.
That puts me in the position of having to override all of the otherwise inherited methods to accommodate this difference. Example:
//from WidgetBase, works fine with Alpha and Beta
public function compile(){
foreach($this->data as $name => $value){
//do something
}
}
//and from GammaWidget
public function compile(){
for($i = 0; $i <= $this->instance; $i++){
foreach($this->data[$i] as $name => value){
//do something
}
}
}
My question is, should I maintain my overridden method approach, or add a conditional check on the calling class in the inherited compile() method, and split my code accordingly, keeping both routes in the same inherited method?
Which would be the best practice?
Note: This would apply to about 9 other methods.
Anyhow, as of currently an application I'm working on makes use of a couple classes I've created.
For simplicity:
abstract class WidgetBase{}
class AlphaWidget extends WidgetBase{}
class BetaWidget extends WidgetBase{}
class GammaWidget extends WidgetBase{}
Now, all is fine and good, my abstract WidgetBase has a nice collection of inherited methods/properties and everything is clean. The issue is though, that GammaWidget is complicating things, as it has an additional property of instance (or index) that adds an additional dimension to the array properties it inherits.
That puts me in the position of having to override all of the otherwise inherited methods to accommodate this difference. Example:
//from WidgetBase, works fine with Alpha and Beta
public function compile(){
foreach($this->data as $name => $value){
//do something
}
}
//and from GammaWidget
public function compile(){
for($i = 0; $i <= $this->instance; $i++){
foreach($this->data[$i] as $name => value){
//do something
}
}
}
My question is, should I maintain my overridden method approach, or add a conditional check on the calling class in the inherited compile() method, and split my code accordingly, keeping both routes in the same inherited method?
Which would be the best practice?
Note: This would apply to about 9 other methods.