【问题标题】:What does extends mean in a class declaration? [duplicate]类声明中的扩展是什么意思? [复制]
【发布时间】:2010-11-27 00:34:42
【问题描述】:

可能重复:
Understanding Classes in PHP

我经常在课堂上看到“扩展”,比如

class a
{
  // its content here
}

class b extends a
{
  //other content here
}

extends 的作用是什么?

【问题讨论】:

标签: php oop class


【解决方案1】:

这是 PHP 用于类继承的关键字。 class bclass a 的子类,因此从所有意图和目的来看,它都是 class a 的实例,但也具有在 class b 中定义的附加(扩展)属性。

延伸阅读here.

【讨论】:

    【解决方案2】:

    这是一个称为Inheritance 的OOP 原则。

    B 类从A 类继承(扩展)时,B 类的一个实例会从A 类继承所有功能并可以访问protected(当然还有public)方法和属性通过parent::

    阅读更多关于inheritance in PHP的信息。


    当类共享某些属性时,通常会使用继承。
    例如,我可以有一个类 Person 包含诸如 name子类 CustomerPartner 等信息,它们继承属性 name 但添加一些自定义功能。

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      PHP手册中有对类继承的合理介绍:
      http://www.php.net/manual/en/language.oop5.inheritance.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-25
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        相关资源
        最近更新 更多