PHP 與 Java 一樣,支援物件導向編程,並使用類別和物件作為其核心構建塊。理解這些概念對於掌握 PHP 至關重要。本指南將涵蓋您需要了解的有關 PHP 中的類別和物件的所有內容。
PHP 中的類別是建立物件的藍圖。它定義了類別的物件將具有的結構和行為。
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
物件是類別的實例。您可以使用 new 關鍵字從類別建立物件。
$objectName = new ClassName();
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
屬性(也稱為欄位)表示物件的狀態,而方法定義物件的行為。
屬性是保存物件資料的變數。
class Car { public $color; public $model; public $year; }
方法是在類別中定義的函數,用來描述物件的行為。
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
建構子是實例化物件時自動呼叫的特殊方法。他們初始化新創建的物件。
如果沒有定義建構函數,PHP 會提供一個不帶參數的預設建構子。
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
參數化建構函式可讓您使用特定值初始化物件。
class Car { public $color; public $model; public $year; // Parameterized constructor public function __construct($color, $model, $year) { $this->color = $color; $this->model = $model; $this->year = $year; } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Main { public function run() { $myCar = new Car("Red", "Tesla", 2022); $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 本身並不像 Java 那樣支援方法重載,但您可以使用可選參數或透過在單一建構函式中手動處理參數來模擬它。
class Car { public $color; public $model; public $year; // Simulating constructor overloading public function __construct($color = "Unknown", $model = "Unknown", $year = 0) { $this->color = $color; $this->model = $model; $this->year = $year; } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Main { public function run() { $defaultCar = new Car(); $defaultCar->displayInfo(); $myCar = new Car("Red", "Tesla", 2022); $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 中的封裝是將資料(屬性)和方法(函數)捆綁在一個類別中的做法。它確保物件的內部狀態不受外界幹擾和誤用。
PHP 中的存取修飾符控制屬性、方法和建構函式的可見性和可存取性。 PHP 支援三種主要的存取修飾符:
class ClassName { // Properties (Fields) // Methods }
PHP 中的靜態成員與類別本身相關聯,而不是與任何特定實例相關聯。無需創建類別的物件即可存取它們。
靜態屬性在類別的所有實例之間共用。它們是使用 static 關鍵字聲明的。
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
靜態方法是使用 static 關鍵字聲明的。它們屬於類別而不是實例。
$objectName = new ClassName();
靜態成員是使用類別名稱存取的,而不是透過物件。
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 中的存取修飾符控制類別成員的可見性,確保封裝並強制執行存取限制。
class Car { public $color; public $model; public $year; }
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
PHP 中的非存取修飾符修改類別成員的行為而不影響其可見性。
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
$objectName = new ClassName();
PHP 中的繼承是一種機制,其中一個類別(子類別)可以繼承另一個類別(超類別)的屬性和方法。它促進程式碼重複使用並允許在類別之間創建層次關係。
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
class Car { public $color; public $model; public $year; }
在此範例中:
PHP 中的存取修飾符決定子類別和程式其他部分中類別成員的可見性。它們在繼承中發揮關鍵作用。
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
PHP 中的靜態成員與類別相關聯,而不是與任何特定實例相關聯。它們遵循與繼承中的非靜態成員相同的存取規則。
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
靜態方法在 PHP 中是繼承的,但不能像實例方法一樣被重寫。當子類別定義同名靜態方法時,會隱藏父類別的靜態方法。
class ClassName { // Properties (Fields) // Methods }
PHP 中的抽象方法必須定義在抽象類別中。抽象方法在超類別中的可見性決定了它在子類別中的可見性。子類別必須實作具有相同或較少限制的存取修飾符的抽象方法。
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
PHP 中的 Final 方法不能被子類別覆蓋,且 Final 類別不能擴展。
$objectName = new ClassName();
在 PHP 中,頂層類別(未嵌套在其他類別中的類別)的聲明遵循特定的關鍵字順序。宣告可以包含存取修飾符、abstract 或final 關鍵字以及class 關鍵字。
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
$objectName = new ClassName();
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
class Car { public $color; public $model; public $year; }
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Car { public $color; public $model; public $year; // Parameterized constructor public function __construct($color, $model, $year) { $this->color = $color; $this->model = $model; $this->year = $year; } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
class Main { public function run() { $myCar = new Car("Red", "Tesla", 2022); $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 中的抽象類別與 Java 中的抽象類別類似,用於定義其他類別的藍圖。它們包含抽象方法(沒有實現的方法)和具體方法(有實現的方法)。抽象類別使用abstract關鍵字聲明,不能直接實例化。
抽象類別充當衍生類別的基底類別。它為其子類別定義了常見行為,同時允許在這些子類別中實作特定方法。
要在 PHP 中宣告抽象類,請在 class 關鍵字之前使用abstract 關鍵字。
class ClassName { // Properties (Fields) // Methods }
抽象方法在抽象類別中定義,但沒有方法體。子類別必須實作所有抽象方法。
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
抽象類別中的具體方法具有主體,可以由子類別按原樣繼承或重寫。
$objectName = new ClassName();
抽象類別的子類別必須實作其所有抽象方法,除非子類別也宣告為抽象。
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
抽象類別不能直接實例化。您必須使用具體的子類別來建立實例。
class Car { public $color; public $model; public $year; }
抽象類別可以有建構函數,它們的建構子在子類別實例化時被呼叫。
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
抽象類別可以包含實例變數和具體方法,為子類別提供可重複使用的功能。
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
抽象類別可以包含靜態方法。靜態方法屬於類,無需實例化即可呼叫。
class ClassName { // Properties (Fields) // Methods }
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
$objectName = new ClassName();
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
PHP 中的介面為實作它的類別定義了一個契約。它指定類別必須實作的方法,但本身不提供任何方法實作。介面允許更靈活和模組化的程式碼,使類別能夠遵守一組通用的方法簽名,無論其繼承層次結構如何。
PHP中的介面類似於抽象類,但它只能定義方法簽章而沒有任何實作。實作介面的類別必須提供介面中聲明的所有方法的實作。一個類別可以實現多個接口,這使得接口成為 PHP 支援行為多重繼承的關鍵部分。
要聲明接口,請使用interface關鍵字。介面只能包含方法宣告(沒有方法體)、常數和抽象方法。
class Car { public $color; public $model; public $year; }
實作介面的類別必須提供介面中聲明的所有方法的實作。一個類別可以實現多個接口,接口之間用逗號分隔。
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
介面中的方法不能有主體,而且它們必須是公共的。當類別實作介面時,它必須完全按照介面中宣告的方式實作這些方法,包括方法簽章。
class Car { public $color; public $model; public $year; // Default constructor public function __construct() { } public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
PHP中的一個類別可以實作多個介面。這使得設計系統時更加靈活,其中類別可以遵守不同的契約。
class ClassName { // Properties (Fields) // Methods }
介面可以包含常數。這些常數自動是公共的,並且可以被任何實作該介面的類別存取。
class Car { // Properties public $color; public $model; public $year; // Methods public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
一個介面可以擴充另一個介面。這意味著子介面繼承了父介面的所有方法(簽名),並且還可以添加新的方法。實作子介面的類別必須實作父介面和子介面的所有方法。
$objectName = new ClassName();
介面不能包含靜態方法。接口中聲明的所有方法都必須是實例方法。介面中不允許使用靜態方法,因為介面為實作類別定義了實例級契約。
class Main { public function run() { $myCar = new Car(); // Creating an object of the Car class $myCar->color = "Red"; $myCar->model = "Tesla"; $myCar->year = 2022; $myCar->displayInfo(); } } $main = new Main(); $main->run();
class Car { public $color; public $model; public $year; }
class Car { public $color; public $model; public $year; public function displayInfo() { echo "Model: " . $this->model . "\n"; echo "Color: " . $this->color . "\n"; echo "Year: " . $this->year . "\n"; } }
以上是PHP、類別和對象的詳細內容。更多資訊請關注PHP中文網其他相關文章!