Is there a way to achieve multiple inheritance in php?

Suppose I have a parent class

class parent { } ..... 

This parent has three subclasses

 class child1 { } class child2 { } class child3 { } 

and these child classes have smaller parts such as

 class child1subpar1 { } class child1subpar2 { public function foo() { echo "hi"; } } class child2subpar1 { } class child2subpar2 { } 

Now how to sum it all up how

 class child1 extends child1subpar1, child1subpar2 { } class child2 extends child2subpar1, childsubpar1 { } class parent extends child1,child2,child3 { } 

I need to execute methods in my inherited classes and do something like this

$ objparent = new parent; $ objparent β†’ foo ();

+4
source share
6 answers

No, but multiple inheritance is usually considered bad practice. Instead, you should use composition, so you simply use class instances that you want to inherit from within your class.

And now, when I look again at your question, this is not even a problem of inheritance, you should use composition. Perhaps if you provide more detailed information about what you expect from this class, we should respond more accurately.

UPDATE:

You will need to create one method for each of the methods of these classes that you want to use is the facade template template. Or maybe you don’t know that you can call methods of internal objects like this:

 $parent->subObject->subSubObject->wantedMethod(); 

Facade:

http://en.wikipedia.org/wiki/Facade_pattern

in your case, the facade will not be anything other than creating one class and defining each method that you want to use, and inside this method, any method of any of your internal instances is called. But I really don't see any benefit coming from this, instead of calling instances and methods hierarchically

+2
source

It looks like you are really confused in OOP.

The parent class does not know its children. If you want to execute a child class, you need to instantiate it.

Multiple inheritance is also not allowed in PHP (like many other popular languages ​​such as Java).

It might be worth looking at the aggregation β€” passing the smaller subclass to the parent class child or event. In addition, you can use various interfaces to force subclasses to have a set of necessary methods.

+2
source

What you do is really the opposite. Inheritance is used to provide common, common functions for objects without duplicating code. Inheritance occurs from parent to child, everything that a parent can do, a child can also do, but he can do more (he extends the functionality of the parent).

 class Parent { function everyoneCanDoThis() { } } class Child extends Parent { // I can implicitly use the everyoneCanDoThis() function function onlyChildrenCanDoThis() { } } 

Since this is a top-down structure, the parent should not rely on any particular Child. The Parent does not perform or does not invoke the functions of the Child. Only you call Child functions, but these functions can be inherited from the parent class.

You must put everything you want so that each object can be in the parent class. Specific functionality that relates only to a specific object passes into the Child.

Multiple inheritance is a different feature of worms that is not possible in PHP, for good reasons. Return to composition as suggested elsewhere here when you get the basics of inheritance. :)


Composition means that you take several objects and save links to them in another object. This has nothing to do with inheritance, since each of these objects may or may not inherit from the Parent class, and they are still separate objects.

 class ComposedObject { private $part1 = null; private $part2 = null; public function __constructor() { $this->part1 = new Part1(); $this->part2 = new Part2(); } public function doTask() { return $this->part1->doSomeTask(); } public function doOtherTask() { return $this->part2->doSomeOtherTask(); } } 

ComposedObject does not have the function itself, inherited or otherwise. But it contains two other objects, each of which has some functionality. The functionality of the β€œparts” can be displayed directly, therefore they are called as $composedObject->part1->doSomeTask() , they can be encapsulated as in the example, therefore they are called as $composedObject->doTask() and internally delegated, or you can use some __call() trickery to automatically delegate functions called by the compiled object to one of its "parts". However, this is a multiple inheritance issue; if the two "parts" contain a method with the same name as you name?

+1
source

+1 to others. You really have it in the opposite direction. Children ALWAYS expand their parents.

But there is even something that can act as multiple inheritance in PHP: Pattern Decorator. I wrote an article about this on my blog here .

+1
source

A class can implement more than one interface, which is slightly different.

When you inherit a parent, you get everything that he has if you do not decide to redefine something, plus you can expand it by adding more more specific things, but the parent does not need to know anything about the child.

When implementing the interface, the interface defines methods, but does not implement them. It is up to you to implement it. Different classes may implement interface methods, but they want it while they follow what the interface says.

Inheritance tends to be excessive and leads to poor programs. Perhaps if you tell us what problem you are trying to solve, one of us may suggest how you can structure your classes.

0
source

Using an interface in php might solve the issue.

0
source

Source: https://habr.com/ru/post/1309495/


All Articles