Asked  7 Months ago    Answers:  5   Viewed   35 times

Is Multiple Inheritance allowed at class level in PHP?



Multiple inheritance suffers from the Diamond Problem, which has not been (agreed upon how to be) solved in PHP yet. Thus, there is no multiple inheritance in PHP.

 ClassA  ClassB

If both ClassA and ClassB defined their own method foo(), which one would you call in ClassC?

You are encouraged to either use object composition or interfaces (which do allow multiple inheritance) or - if you are after horizontal reuse - look into the Decorator or Strategy pattern until we have Traits (or Grafts or whatever they will be called then).

Some Reference:

  • [PHP-DEV] Traits,Grafts, horizontal reuse
  • [PHP-DEV] Multiple class inheritance
  • RFC: Traits for PHP
  • Traits-like Functionality in PHP now
Wednesday, March 31, 2021
answered 7 Months ago

Either use Interfaces and implement the methods manually or via Strategies. Or use Composition instead of Inheritance, meaning you let the Order_Product have a Order_Item and a Cart_Product.

On a sidenote: You could also consider making "shipping calculations" into it's own Service class that you can pass appropriate Product instances to.

Wednesday, March 31, 2021
answered 7 Months ago

Edit: Based on your comments, you want something like this:

class A{
    final public function say(){
        echo "hello<br>";

    public function _say(){
        //By default, do nothing

class B extends A{
    public function _say(){
        echo "Hi<br>";

I have called the new function _say, but you can give it any name you want. This way, your teammates just define a method called _say(), and it will automatically be called by class A's say method.

This is called the Template method pattern

Old answer

Just add parent::say(); to the overloading method:

class B extends A{
    public function say(){
        echo "hi<br>";

This tells php to execute the overloaded method. If you don't want extending classes to overload it's methods, you can declare them final.

See also the php manual about extending classes.

Wednesday, March 31, 2021
answered 7 Months ago

That will never work, because 'param' is not a property of A: it is in c, which is a property of A.

What you need to do is define the magic methods such as __set and __get, which parallel __call for properties.

Saturday, May 29, 2021
answered 5 Months ago

Quoting Java Generics: extends, super and wildcards explained:

The super bound is not allowed in class definition.

//this code does not compile !
class Forbidden<X super Vehicle> { }

Why? Because such construction doesn't make sense. For example, you can't erase the type parameter with Vehicle because the class Forbidden could be instantiated with Object. So you have to erase type parameters to Object anyway. If think about class Forbidden, it can take any value in place of X, not only superclasses of Vehicle. There's no point in using super bound, it wouldn't get us anything. Thus it is not allowed.

Thursday, July 29, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :