Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to extend a PHP class from two classes one of which is abstract [duplicate]

Can I extend a PHP class from two classes, one of which is abstract and other one is not? like:

class customer extends SomeControllerClass implements SomeAbstractClass {
...
}

the reason to do is that I have my commonly used functions and logic in the abstract class.

like image 699
Firdous Avatar asked Mar 02 '12 11:03

Firdous


People also ask

Can we extend more than one abstract class in PHP?

No you can't, respectively, not really, as manual of extends keyword says: An extended class is always dependent on a single base class, that is, multiple inheritance is not supported.

Can you extend 2 classes in PHP?

Classes, case classes, objects, and traits can all extend no more than one class but can extend multiple traits at the same time.

Can an abstract class extend another abstract class PHP?

In most cases, an abstract class will contain at least one abstract method though it is not required. If a class contains one or more abstract methods, it must be an abstract class. If a class extends an abstract class, it must implement all abstract methods or itself be declared abstract.

Can two classes extend one abstract class?

You can only Extend a single class. And implement Interfaces from many sources. Extending multiple classes is not available.


2 Answers

No. But there are traits in PHP 5.4 that you can use for that.

like image 133
meze Avatar answered Oct 20 '22 08:10

meze


Think about this: do you only want inheritance for reusability, or is there an is-a relationship? if it is just the former, then delegation is a better solution. For the latter you will need to use interfaces and probably delegation again to achieve this. So the answer is use delegation.

like image 24
Robert Avatar answered Oct 20 '22 09:10

Robert