Davit
Davit

Reputation: 1373

Real multiple class inheritance, not a collection of methods via traits

I understand the way you extend a class like ChildClass extends ParentClass, I understand use of class abstraction and interfaces, and I do understand traits

In the articles I have read most of people stated that traits is used in practice to achieve multiple inheritance that is not done by extends functionality as it supports only single extension.

As I understand traits it is more of a collection of functions that can be used by different classes or other traits.

In this question I ask you to tell me how is it correctly done to literally do extension of more than one class not adding some function-collection.

Upvotes: 0

Views: 33

Answers (1)

Evert
Evert

Reputation: 99533

I can be extremely clear about this:

PHP does not allow this.

I indeed believe that traits provides a way to achieve multiple inheritance, under some restricted conditions. If your codebase is extremely OOP pure and well designed, traits may allow you to achieve multiple inheritence.

But it's not a catch all. If this is a stumbling block for you, don't blame this on PHP's design, you probably need to think about a different way to do what you want. (for example: delegate instead of sub-class).

Upvotes: 5

Related Questions