March 22, 2012

How to extend multiple utility classes

Question by w00

This question is kinda aimed towards PHP but it probably applies to other languages aswell. In PHP you can only extend one class. But what if you need more classes?

Suppose i have the following classes:

class MyClass extends Observer, Logger, OtherUtilClass

MyClass can’t extend more than one class. But it needs to be an observer. And it might need some other base class aswell to fully function. What would be the best approach for this?

Answer by Starx

No

Your idea leads to multiple inheritance and it is not available in PHP for good.

I extremely recommended you read this question to see why you shouldn’t try this.


However, you still can chain the classes. If done in a proper way you can get the functionality of all classes.

Class Observer {}
Class Logger extends Observer {}
Class OtherUtilClasses extends Logger {}

//Now
class MyClass extends OtherUtilClasses {}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!