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.
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.
Classes, case classes, objects, and traits can all extend no more than one class but can extend multiple traits at the same time.
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.
You can only Extend a single class. And implement Interfaces from many sources. Extending multiple classes is not available.
No. But there are traits in PHP 5.4 that you can use for that.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With