简体   繁体   English

PHP:为什么不能扩展多个类?

[英]PHP: Why isn't it possible to extend more than one class?

I've faced a situation when I want to extend two parent classes, but php does not allow this. 当我想扩展两个父类时,我遇到了一种情况,但php不允许这样做。
Why I can't extend more than one class, but can implement more than one interface . 为什么我不能extend多个类,但可以implement多个interface What's wrong with extending many classes? 扩展许多课程有什么问题?
It seemed to me like a pretty obvious thing, until I got parse errors. 在我得到解析错误之前,在我看来这很明显。
Is it a bad practice? 这是一种不好的做法吗? If so, what are the alternatives? 如果是这样,有哪些替代方案?
Is it possible in other languages? 是否可以用其他语言?

You're probably looking for: Multiple Inheritance in PHP . 您可能正在寻找: PHP中的多重继承

It seems to be possible in Python. 它似乎可以在Python中使用。

Why multiple inheritance is forbidden in some/most programming languages is argued with the diamond problem http://en.wikipedia.org/wiki/Diamond_problem . 为什么在某些/大多数编程语言中禁止多重继承与钻石问题http://en.wikipedia.org/wiki/Diamond_problem争论。

Put simple if you have a car that can swim and drive because it inherits from vehicle and boat what happens on execution of the move function!? 如果你有一辆可以游泳和开车的汽车那就简单了,因为它继承了车辆和船只在执行移动功能时会发生什么!

Try using interfaces and follow the Strategy pattern or State pattern. 尝试使用接口并遵循策略模式或状态模式。

Is it a bad practice? 这是一种不好的做法吗? If so, what are alternatives? 如果是这样,有什么替代方案?

Unless the language is specifically designed for it, yes. 除非语言是专门为它设计的,否则。 Consider, you have two classes, A and B . 考虑一下,你有两个类, AB Both classes provide a public method foo() which have identical signatures (not hard in PHP). 这两个类都提供了一个公共方法foo() ,它具有相同的签名(在PHP中并不难)。 Now, you make a class C which extends both A and B . 现在,你创建了一个扩展AB C类。

Now, you call C.foo() . 现在,你调用C.foo() Without explicit instructions, how does the interpreter know which version of foo() to call? 如果没有明确的指示,解释器如何知道要调用哪个版本的foo()

It's not supported by PHP. 它不支持PHP。 It can however be simulated using runkit, APD or by just overriding __call and __get to simulate inheritance from multiple classes. 但是,它可以使用runkit,APD或仅重写__call和__get来模拟来模拟多个类的继承。 Symfony (and I seldomly recommand that) also provides "sfMixin" or " sfMixer " for multiple inheritance. Symfony(我很少推荐)也为多重继承提供了“sfMixin”或“ sfMixer ”。

Separate classes implementing the same method is not a good argument against multiple inheritance, as currently multiple interfaces can be implemented. 实现相同方法的单独类不是反对多重继承的好参数,因为当前可以实现多个接口。 You just can't implement two interfaces with the same method. 您无法使用相同的方法实现两个接口。 Quote from http://www.php.net/manual/en/language.oop5.interfaces.php : "A class cannot implement two interfaces that share function names, since it would cause ambiguity." 引自http://www.php.net/manual/en/language.oop5.interfaces.php :“一个类不能实现两个共享函数名的接口,因为它会引起歧义。”

I know this question is 2 years old but I've only just had the same problem. 我知道这个问题是2岁,但我只是遇到了同样的问题。 My work-around is this: if you have one regular class and two abstracts and want to extend both, eg abstract class AbstractOne and abstract class AbstractTwo , you can say: 我的解决方法是:如果你有一个常规类和两个摘要并想要扩展它们,例如abstract class AbstractOneabstract class AbstractTwo ,你可以说:

abstract class  AbstractOne extends  AbstractTwo {

}

Then add to the main class like this: 然后像这样添加到主类:

class MyMainClass extends  AbstractOne {

}

This way, it inherits both AbstractOne and AbstractTwo. 这样,它继承了AbstractOne和AbstractTwo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM