0

Let's take the following class structure:

class A {
    ... has all the functionalities of A
}

class B extends A {
   ... has all the functionalities of A and B
}

And these two other class structures:

class C extends B {
   ... has all the functionalities of A and B and C
}

class D extends A {
    ... all the functionalities of A, and D
}

how could I create a class easily, that would gain all the functionalities of all four of my classes, if I am not allowed to modify class A or class B? Would something like this be possible with PHP?

EDIT:

The reason I would like to do this is the following, I am open for suggestions on other ways my desired outcome can be achieved:

I have a module, which has several classes on which I plan to build on, and I do not want to edit the module directly, but I would like to add functionalities potentially to multiple classes of this module (this is where class A and class B is coming from).

So to edit class A, I would create a class D, which extends it, and add new functionalities, or rewrite already added functionalities that needs rewrite in class D.

But there are multiple classes in this module, which are simmilar in structure to class B, which I would also like to potentially modify, hence my class C. But if I modified the modules class A in my class D, I would need my new class C to extend the class D instead of the class A. (hope you can still follow me:P)

Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • Multiple inheritance in php is not available. But `D` can extend `C` and your new class can extend `D`. But I think this is a very poor design. Or your new class will extend `D` and you inject instance of `C` in it. – u_mulder Jan 16 '20 at 08:43
  • You can use protected class, any SO reference [here](https://stackoverflow.com/questions/4361553/what-is-the-difference-between-public-private-and-protected) – Simone Rossaini Jan 16 '20 at 08:44
  • @u_mulder I have edited my question, to show why I am contemplating on taking this path, but I am open to other advices on how to solve my problem... – Adam Baranyai Jan 16 '20 at 08:51
  • What about using interfaces? – B001ᛦ Jan 16 '20 at 09:07
  • @B001ᛦ I don't see how interfaces would help me in this situation... would you elaborate on this please? – Adam Baranyai Jan 16 '20 at 09:13
  • _would you elaborate on this please..._ Take a look at the SOLID principles. You will find out that inheritance is not always the best approach--almost never – B001ᛦ Jan 16 '20 at 11:19

1 Answers1

2

No, in PHP it is not possible to inherit from multiple parents due to "Dimond Problem". In your case, this means you can extend c for the functionality of a, b & c, but you cannot extend d too. Since you cannot modify the other classes, there is not right solution here, but I'd recommend looking into traits (https://www.php.net/manual/en/language.oop5.traits.php), as these allow you to 'inherit' from multiple traits.

ezio4df
  • 3,541
  • 6
  • 16
  • 31
MultiSuperFreek
  • 417
  • 3
  • 8
  • looks interesting, and maybe exactly what I need, I will look into it, and if it solves my issue, I will accept the answer – Adam Baranyai Jan 16 '20 at 08:53