tags:

views:

104

answers:

4

Hi,

I'm working with a framework, but for sometimes I need to alter some methods by overloading classes.

My problem is when a class B inherits from a class A and where I need to overload them both, eg:

class B extends A {}

First I overload A and B, in order to alter some of their methods:

class AA extends A {}
class BB extends B {}

But then what to do for BB extends from AA

In others words ('<--' means extends):

class B   <---  class A
   ^               ^
   |               |
   |               |
class BB  <-?-  class AA
+3  A: 

i think you might be looking for Composition

Question Mark
+1  A: 

Yes. This is where Traits or Composition comes to play.

Basically, traits in programming concept is a group of functionality that you can include into any class. Think of traits as just a set of functions you can add to your classes.

Aaron Qian
A: 

What you want to accomplish is called multiple inheritance, but that is not supported by PHP and most other recent languages. There is rarely a good reason to use multiple inheritance, and since it can be confusing, it is generally frowned upon.

As mentioned, you can probably achieve what you want in à clean wat through composition, but without more information about your classes, it is impossible to give Amy specific advice.

Thorarin
A: 

A good rule of thumb is always prefer composition over inheritance. Once you start thinking that way and almost always using composition, the decision when to use inheritance becomes pretty obvious.

Bill K