Is there any way to achieve multiple inheritance in php?

蓝咒 提交于 2020-01-03 17:16:49

问题


Lets say I have a parent class

class parent { }

.....

This parent has three sub class

class child1 { }

class child2 { }

class child3 { }

and these child classes have further smaller parts like

class child1subpar1 { }

class child1subpar2 {

       public function foo() {
          echo "hi";
       }

}

class child2subpar1 { }

class child2subpar2 { }

Now, how to sum this whole up like

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

class parent extends child1,child2,child3 { }

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

$objparent = new parent; $objparent -> foo();


回答1:


No, but multiple inheritance is generally considered a bad practice. You should favor composition instead, so you just use instances of classes you wanted to inherit inside your class.

And now when I look into your question again, it's not even an inheritance issue, you should use composition. Maybe if you provided more detail what you expect that class to do, we should answer more accurately.

UPDATE:

You will need to create one method for each of these classes' method which you would want to use - it's called Facade design pattern. Or maybe you are not aware that you can call methods of inner objects like this:

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

Facade pattern:

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

in your case facade wouldn't be anything else than creating one class and define every single method you want to use, and inside that method calling any method of any of your inner instances. But i really don't see any benefit coming from this instead of calling instances and methods hierarchically




回答2:


Seems like you're really confused about OOP.

Parent class has no awareness of its children. If you want to execute a child class, you need to create its instance.

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

It might be worth looking at aggregation - passing smaller sub classes into child or event parent class. also, you can use implement multiple interfaces to force subclasses to have a set of required methods.




回答3:


What you're doing is really backwards. Inheritance is used to bestow common, shared functionality upon objects without code duplication. The inheritance goes from Parent to Child, everything the Parent can do, the Child can do as well, but it may do more (it 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 specific Child. The Parent does not execute or call functions of a Child. Only you call functions of a Child, but these functions may be inherited from a Parent class.

You should put everything you want every object to be able to do in a Parent class. Specific functionality that's only relevant to a specific object goes into a Child.

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


Composition just means that you take several objects and hold references to them in another object. It has nothing to do with inheritance, as each of these objects may or may not inherit from a Parent class and they're still individual 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();
    }

}

The ComposedObject does not have any functionality itself, inherited or otherwise. But it holds two other objects that each carry some functionality. The functionality in the "parts" may be exposed directly, so they're called like $composedObject->part1->doSomeTask(), they may be encapsulated as in the example, so they're called like $composedObject->doTask() and are internally delegated, or you may use some __call() trickery to automatically delegate functions called on the composed object to one of its "parts". This has the same problem as multiple inheritance though; if two "parts" both contain a method of the same name, which one do you call?




回答4:


+1 to the others. You really have it backwards. Children ALWAYS extends their parents.

But there is even something that can act kind of like multiple inheritance in PHP: The Decorator Pattern. I wrote an article about it on my blog here.




回答5:


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

When you inherit from a parent, you get everything it has unless you choose to override something, plus you can extend it by adding additional more specific stuff, but the parent should not know anything about the child.

When you implement an interface, the interface defines methods but it doesn't implement them. It is up to you to implement it. Different classes can implement the interface methods however they want as long as they follow what the interface says.

Inheritance tends to be overused and leads to bad programs. Perhaps if you told us what problem you're trying to solve, one of us could suggest how you can structure your classes.




回答6:


Using Interface in Php maybe solve the question.



来源:https://stackoverflow.com/questions/2815568/is-there-any-way-to-achieve-multiple-inheritance-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!