在PHP的类级别上允许多重继承吗? [英] Is Multiple Inheritance allowed at class level in PHP?

查看:284
本文介绍了在PHP的类级别上允许多重继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP是否在类级别允许多重继承?

Is Multiple Inheritance allowed at class level in PHP?

推荐答案

钻石问题,尚未在PHP中解决(同意如何使用).因此, PHP中没有多重继承.

Multiple inheritance suffers from the Diamond Problem, which has not been (agreed upon how to be) solved in PHP yet. Thus, there is no multiple inheritance in PHP.

    BaseClass
       /\
      /  \
 ClassA  ClassB
      \  /
       \/
     ClassC

如果ClassAClassB都定义了自己的方法foo(),您将在ClassC中调用哪个方法?

If both ClassA and ClassB defined their own method foo(), which one would you call in ClassC?

鼓励您使用对象组成装饰器策略模式,直到我们特质(或

You are encouraged to either use object composition or interfaces (which do allow multiple inheritance) or - if you are after horizontal reuse - look into the Decorator or Strategy pattern until we have Traits (or Grafts or whatever they will be called then).

一些参考:

  • [PHP-DEV] Traits,Grafts, horizontal reuse
  • [PHP-DEV] Multiple class inheritance
  • RFC: Traits for PHP
  • Traits-like Functionality in PHP now

这篇关于在PHP的类级别上允许多重继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆