php中有没有办法实现多重继承?
假设我有一个父类,
class parent { }
.....
该父类有三个子类
class child1 { }
class child2 { }
class child3 { }
,这些子类还有更小的部分,例如
class child1subpar1 { }
class child1subpar2 {
public function foo() {
echo "hi";
}
}
class child2subpar1 { }
class child2subpar2 { }
现在,如何总结整个内容,就像
class child1 extends child1subpar1, child1subpar2 { }
class child2 extends child2subpar1, childsubpar1 { }
class parent extends child1,child2,child3 { }
我需要执行其继承类中的方法并执行类似
$objparent = new 的操作父母; $objparent ->; foo();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
看来你对 OOP 真的很困惑。
父类对其子类没有意识。如果要执行子类,则需要创建其实例。
PHP(以及许多其他流行语言,如 Java)中也不允许多重继承。
可能值得考虑聚合 - 将较小的子类传递给子类或事件父类。此外,您可以使用实现多个接口来强制子类具有一组所需的方法。
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.
你所做的事情实在是本末倒置。继承用于为对象赋予通用、共享的功能,而无需重复代码。继承从父级到子级,父级可以做的所有事情,子级也可以做,但它可能会做更多(它扩展父级的功能)。
由于这是自上而下的结构,因此父级不应该依赖于任何特定的子级。父级不执行或调用子级的函数。只有您调用子类的函数,但这些函数可能是从父类继承的。
您应该将您希望每个对象能够执行的所有操作都放在父类中。仅与特定对象相关的特定功能进入子对象。
多重继承是一种不同的蠕虫病毒,在 PHP 中是不可能的,这是有充分理由的。当您了解了继承的基础知识后,回到组合,正如这里其他地方所建议的那样。 :)
组合只是意味着您采用多个对象并在另一个对象中保存对它们的引用。它与继承无关,因为每个对象可能会也可能不会从父类继承,并且它们仍然是单独的对象。
CompositedObject 本身没有任何功能,无论是继承的还是其他的。但它还包含另外两个对象,每个对象都具有一些功能。 “部件”中的功能可能会直接暴露,因此它们的调用方式类似于
$composedObject->part1->doSomeTask()
,它们可能会像示例中那样进行封装,因此它们'像$composedObject->doTask()
一样重新调用,并在内部委托,或者您可以使用一些__call()
诡计 自动将组合对象上调用的函数委托给其“部分”之一。但这与多重继承有同样的问题;如果两个“部分”都包含同名的方法,那么您调用哪一个?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).
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.
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?给其他人+1。你真是搞反了。孩子总是延伸父母。
但甚至有一些东西可以起到类似于 PHP 中的多重继承的作用:装饰器模式。
我写了一篇关于它的文章 博客在这里。
+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.
一个类可以实现多个接口,这是一件略有不同的事情。
当您从父级继承时,您将获得它所拥有的一切,除非您选择覆盖某些内容,而且您可以通过添加其他更具体的内容来扩展它,但父级不应该知道有关子级的任何信息。
当您实现一个接口时,该接口定义了方法,但并不实现它们。由你来实施它。不同的类可以按照自己的意愿实现接口方法,只要遵循接口的规定即可。
继承往往会被过度使用并导致糟糕的程序。也许如果您告诉我们您想要解决什么问题,我们中的一个人可以建议您如何构建您的课程。
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.
在 PHP 中使用 Interface 也许可以解决这个问题。
Using Interface in Php maybe solve the question.
不,但多重继承通常被认为是一种不好的做法。相反,您应该倾向于组合,因此您只需使用要在类中继承的类的实例。
现在,当我再次研究你的问题时,它甚至不是继承问题,你应该使用组合。也许如果您提供更多您希望该课程做什么的详细信息,我们应该回答更准确。
更新:
您将需要为您想要使用的每个类的方法创建一个方法 - 它称为外观设计模式。或者您可能不知道可以像这样调用内部对象的方法:
外观模式:
http:// en.wikipedia.org/wiki/Facade_pattern
在你的情况下,门面除了创建一个类并定义你想要使用的每一个方法,并在该方法内调用任何内部实例的任何方法之外,没有其他任何东西。但我真的没有看到这比打电话有任何好处
实例和方法分层
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:
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