菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

PHP 教程

PHP HOME PHP 简介 PHP 安装 PHP 语法 PHP 注释 PHP 变量 PHP Echo / Print PHP 数据类型 PHP 字符串 PHP 数字 PHP 类型转换 PHP 数学 PHP 常量 PHP 魔术常量 PHP 运算符 PHP If...Else...Elseif PHP Switch PHP 循环 PHP 函数 PHP 数组 PHP 超全局变量 PHP 正则表达式

PHP 表单

PHP 表单处理 PHP 表单验证 PHP 表单必填项 PHP 表单 URL/电子邮件 PHP 表单完成

PHP 高级

PHP 日期和时间 PHP Include PHP 文件处理 PHP 文件打开/读取 PHP 文件创建/写入 PHP 文件上传 PHP Cookies PHP Sessions PHP 过滤器 PHP 高级过滤器 PHP 回调函数 PHP JSON PHP 异常

PHP OOP

PHP 什么是 OOP PHP 类/对象 PHP 构造函数 PHP 析构函数 PHP 访问修饰符 PHP 继承 PHP 常量 PHP 抽象类 PHP 接口 PHP Trait PHP 静态方法 PHP 静态属性 PHP 命名空间 PHP 可迭代对象

MySQL 数据库

MySQL 数据库 MySQL 连接 MySQL 创建数据库 MySQL 创建表 MySQL 插入数据 MySQL 获取最后 ID MySQL 插入多条数据 MySQL 预处理 MySQL 查询数据 MySQL Where MySQL Order By MySQL 删除数据 MySQL 更新数据 MySQL 限制数据

PHP XML

PHP XML 解析器 PHP SimpleXML 解析器 PHP SimpleXML - 获取 PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX 简介 AJAX PHP AJAX 数据库 AJAX XML AJAX 实时搜索 AJAX 投票

PHP 示例

PHP 示例 PHP 编译器 PHP 测验 PHP 练习 PHP 服务器 PHP 证书

PHP 参考手册

PHP 概述 PHP 数组 PHP 日历 PHP 日期 PHP 目录 PHP 错误 PHP 异常 PHP 文件系统 PHP 过滤器 PHP FTP PHP JSON PHP 关键词 PHP Libxml PHP 邮件 PHP 数学 PHP 杂项 PHP MySQLi PHP 网络 PHP 输出控制 PHP 正则表达式 PHP SimpleXML PHP Stream PHP String PHP 变量处理 PHP XML 解析器 PHP 压缩 PHP 时区

PHP OOP - 继承


PHP - 什么是继承?

OOP 中的继承 = 一个类派生于另一个类。

子类将继承父类的所有公共和受保护的属性和方法。此外,它还可以拥有自己的属性和方法。

通过使用 extends 关键字来定义一个继承的类。

我们来看一个例子

示例

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
  }
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
自己动手试一试 »

示例解释

Strawberry 类继承自 Fruit 类。

这意味着 Strawberry 类可以因为继承而使用 Fruit 类的公共 $name 和 $color 属性以及公共 __construct() 和 intro() 方法。

Strawberry 类还有自己的方法:message()。



PHP - 继承和 Protected 访问修饰符

在前一章中,我们了解到 protected 属性或方法可以在类内部以及从该类派生的类中访问。这是什么意思?

我们来看一个例子

示例

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
  }
}

// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red");  // OK. __construct() is public
$strawberry->message(); // OK. message() is public
$strawberry->intro(); // ERROR. intro() is protected
?>
自己动手试一试 »

在上面的示例中,我们看到如果我们尝试从类外部调用一个 protected 方法(intro()),我们会收到一个错误。public 方法可以正常工作!

我们来看另一个例子

示例

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
    // Call protected method from within derived class - OK
    $this -> intro();
  }
}

$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
$strawberry->message(); // OK. message() is public and it calls intro() (which is protected) from within the derived class
?>
自己动手试一试 »

在上面的示例中,我们看到一切正常!这是因为我们从派生类内部调用了 protected 方法(intro())。


PHP - 重写继承的方法

子类通过重新定义方法(使用相同的方法名)来覆盖继承的方法。

请看下面的示例。子类 (Strawberry) 中的 __construct() 和 intro() 方法将覆盖父类 (Fruit) 中的 __construct() 和 intro() 方法。

示例

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public $weight;
  public function __construct($name, $color, $weight) {
    $this->name = $name;
    $this->color = $color;
    $this->weight = $weight;
  }
  public function intro() {
    echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
  }
}

$strawberry = new Strawberry("Strawberry", "red", 50);
$strawberry->intro();
?>
自己动手试一试 »

PHP - final 关键字

final 关键字可用于阻止类继承或阻止方法覆盖。

下面的示例说明了如何阻止类继承

示例

<?php
final class Fruit {
  // 一些代码
}

// 将导致错误
class Strawberry extends Fruit {
  // 一些代码
}
?>
自己动手试一试 »

下面的示例说明了如何阻止方法覆盖

示例

<?php
class Fruit {
  final public function intro() {
    // 一些代码
  }
}

class Strawberry extends Fruit {
  // 将导致错误
  public function intro() {
    // 一些代码
  }
}
?>
自己动手试一试 »

×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持