選單
×
   ❮     
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 提供支援