บทช่วยสอนPHP

PHP HOME แนะนำ PHP การติดตั้ง PHP PHP ไวยากรณ์ ความคิดเห็น PHP ตัวแปร PHP PHP Echo / พิมพ์ ประเภทข้อมูล PHP PHP Strings หมายเลข PHP PHP คณิตศาสตร์ ค่าคงที่ PHP ตัวดำเนินการ PHP PHP If...Else...Elseif PHP Switch PHP Loops ฟังก์ชัน PHP PHP Arrays PHP Superglobals PHP RegEx

แบบฟอร์มPHP

การจัดการแบบฟอร์ม PHP การตรวจสอบแบบฟอร์ม PHP ต้องใช้แบบฟอร์ม PHP URL แบบฟอร์ม PHP/อีเมล แบบฟอร์ม PHP เสร็จสมบูรณ์

PHPขั้นสูง

วันที่และเวลา PHP รวม PHP การจัดการไฟล์ PHP ไฟล์ PHP เปิด/อ่าน สร้าง/เขียนไฟล์ PHP อัพโหลดไฟล์ PHP คุกกี้ PHP PHP Sessions ตัวกรอง PHP ตัวกรอง PHP ขั้นสูง ฟังก์ชันเรียกกลับของ PHP PHP JSON ข้อยกเว้น PHP

PHP OOP

PHP OOP คืออะไร คลาส PHP/วัตถุ ตัวสร้าง PHP PHP Destructor PHP Access Modifiers PHP Inheritance ค่าคงที่ PHP PHP Abstract Classes อินเทอร์เฟซ PHP PHP ลักษณะ PHP Static Methods PHP คุณสมบัติคงที่ PHP เนมสเปซ PHP Iterables

ฐานข้อมูลMySQL

ฐานข้อมูล MySQL MySQL Connect MySQL สร้าง DB MySQL สร้างตาราง MySQL แทรกข้อมูล MySQL รับ ID ล่าสุด MySQL แทรกหลายรายการ MySQL ที่เตรียมไว้ MySQL Select Data MySQL Where MySQL สั่งซื้อโดย MySQL ลบข้อมูล ข้อมูลอัพเดต MySQL MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser 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 Array ปฏิทิน PHP PHP วันที่ ไดเรกทอรี PHP ข้อผิดพลาด PHP ข้อยกเว้น PHP ระบบไฟล์ PHP ตัวกรอง PHP PHP FTP PHP JSON คีย์เวิร์ด PHP PHP Libxml PHP Mail PHP คณิตศาสตร์ PHP เบ็ดเตล็ด PHP MySQLi เครือข่าย PHP การควบคุมเอาต์พุต PHP PHP RegEx PHP SimpleXML PHP Stream PHP String การจัดการตัวแปร PHP PHP XML Parser PHP Zip เขตเวลา 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 สามารถใช้คุณสมบัติ $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
  }
}
?>