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 สามารถใช้คุณสมบัติ $name และ $color สาธารณะรวมถึงเมธอด __construct() และ intro() สาธารณะจากคลาส Fruit เนื่องจากการสืบทอด
คลาส Strawberry ยังมีเมธอดของตัวเอง: message()
PHP - การสืบทอดและตัวดัดแปลงการเข้าถึงที่ได้รับการป้องกัน
ในบทที่แล้ว เราได้เรียนรู้ว่า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 - แทนที่วิธีการสืบทอด
เมธอดที่สืบทอดมาสามารถแทนที่ได้โดยการกำหนดเมธอดใหม่ (ใช้ชื่อเดียวกัน) ในคลาสย่อย
ดูตัวอย่างด้านล่าง เมธอด __construct() และ intro() ในคลาสย่อย (Strawberry) จะแทนที่เมธอด __construct() และ intro() ในคลาสพาเรนต์ (Fruit):
ตัวอย่าง
<?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
สืบทอดคลาสหรือเพื่อป้องกันการแทนที่เมธอด
ตัวอย่างต่อไปนี้แสดงวิธีการป้องกันการสืบทอดคลาส:
ตัวอย่าง
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
ตัวอย่างต่อไปนี้แสดงวิธีการป้องกันการแทนที่เมธอด:
ตัวอย่าง
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>