メインコンテンツへ移動

เนื้อหาภาษาไทย / タイ語のコンテンツ

システムガイド 内容確認済み

Protected Access Modifier: การเข้าถึงสำหรับ Class ลูก

บทความนี้อธิบายถึง `protected` access modifier ซึ่งอนุญาตให้ Class นั้นๆ และ Class ลูกที่สืบทอดมาจาก Class นั้น สามารถเข้าถึง Properties และ Methods ได้ ต่างจาก...

Protected Access Modifier: การเข้าถึงสำหรับ Class ลูก

จากบทที่แล้ว เราได้เรียนรู้เกี่ยวกับ private access modifier ที่จำกัดการเข้าถึง Properties และ Methods ให้อยู่ภายใน Class ของตัวเองเท่านั้น ในบทนี้ เราจะมาทำความรู้จักกับ protected access modifier ซึ่งเป็นอีกหนึ่งทางเลือกในการควบคุมการเข้าถึง Properties และ Methods โดยจะอนุญาตให้ Class นั้นๆ และ Class ลูก (Child Class) ที่สืบทอดมาจาก Class นั้น สามารถเข้าถึงได้

การประกาศ Protected Properties และ Methods

การประกาศ Properties หรือ Methods ให้เป็น protected ทำได้โดยการเติม keyword protected ไว้หน้าการประกาศ เหมือนกับ public และ private

class Animal {
  protected $name;

  protected function sayHello() {
    echo "Hello from Animal!\n";
  }
}

ในตัวอย่างนี้ ทั้ง $name property และ sayHello() method ถูกประกาศให้เป็น protected

การเข้าถึงจาก Class ลูก

Class ลูกที่สืบทอดมาจาก Class แม่ สามารถเข้าถึง protected Properties และ Methods ได้เหมือนกับเป็นสมาชิกของตัวเอง

class Animal {
  protected $name;

  public function __construct($name) {
    $this->name = $name;
  }

  protected function sayHello() {
    echo "Hello from Animal, my name is " . $this->name . "!\n";
  }
}

class Dog extends Animal {
  public function bark() {
    echo "Woof!\n";
    $this->sayHello(); // เข้าถึง protected method จาก Class แม่
  }

  public function getName() {
    return $this->name; // เข้าถึง protected property จาก Class แม่
  }
}

$dog = new Dog("Buddy");
$dog->bark(); // แสดงผล: Woof! Hello from Animal, my name is Buddy!
echo $dog->getName(); // แสดงผล: Buddy

ในตัวอย่างนี้ Class Dog สืบทอดมาจาก Class Animal Dog สามารถเรียกใช้ sayHello() method ซึ่งเป็น protected และเข้าถึง $name property ซึ่งเป็น protected ได้โดยตรงภายใน Methods ของตัวเอง

ข้อควรระวัง: ถึงแม้ Class ลูกจะสามารถเข้าถึง protected members ได้ แต่ Instance ของ Class แม่ **ไม่สามารถ** เข้าถึง protected members ของ Class ลูกได้

ตัวอย่างการใช้งานจริง

ลองพิจารณา Class Database ที่มี $connection property เก็บ Connection Object ของฐานข้อมูล

class Database {
  protected $connection;

  public function __construct($host, $username, $password, $database) {
    $this->connection = new mysqli($host, $username, $password, $database);

    if ($this->connection->connect_error) {
      die("Connection failed: " . $this->connection->connect_error);
    }
  }

  public function query($sql) {
    return $this->connection->query($sql);
  }
}

class User extends Database {
  public function __construct($host, $username, $password, $database) {
    parent::__construct($host, $username, $password, $database);
  }

  public function getUserById($id) {
    $result = $this->query("SELECT * FROM users WHERE id = " . $id); // ใช้ $this->connection ผ่าน method query()
    return $result->fetch_assoc();
  }
}

// Example Usage (replace with your database credentials)
$user = new User("localhost", "username", "password", "database_name");
$userData = $user->getUserById(1);

if ($userData) {
  echo "User ID: " . $userData['id'] . "\n";
  echo "Username: " . $userData['username'] . "\n";
} else {
  echo "User not found.\n";
}

ในตัวอย่างนี้ $connection property ถูกประกาศเป็น protected ทำให้ Class User ที่สืบทอดมาจาก Database สามารถใช้ Connection Object เพื่อทำการ Query ฐานข้อมูลได้ โดยผ่าน method query() ที่ถูกสืบทอดมา

ข้อดีและข้อเสียของการใช้ Protected

ข้อดี:

  • Code Reuse: อนุญาตให้ Class ลูกสามารถเข้าถึงและใช้ Properties และ Methods ของ Class แม่ได้ ทำให้ลดความซ้ำซ้อนของโค้ด
  • Encapsulation ที่ยืดหยุ่น: protected เป็นจุดกึ่งกลางระหว่าง public และ private อนุญาตให้ Class ลูกเข้าถึงได้ แต่ป้องกันการเข้าถึงโดยตรงจากภายนอก Class
  • การออกแบบที่ชัดเจน: ช่วยให้เห็นภาพความสัมพันธ์ระหว่าง Class แม่และ Class ลูกได้ชัดเจนขึ้น ทำให้การออกแบบระบบมีความยืดหยุ่นและง่ายต่อการบำรุงรักษา

ข้อเสีย:

  • ลด Encapsulation: เมื่อเทียบกับ private, protected ลดระดับการ Encapsulation ลง เนื่องจากเปิดให้ Class ลูกเข้าถึงได้ ซึ่งอาจนำไปสู่การเปลี่ยนแปลงที่ไม่คาดคิด
  • ความซับซ้อนในการ Debug: หากมีการแก้ไข Properties หรือ Methods ที่เป็น protected ใน Class แม่ อาจส่งผลกระทบต่อ Class ลูกหลาย Class ทำให้การ Debug ยากขึ้น
  • การพึ่งพากัน (Coupling): การใช้ protected มากเกินไปอาจทำให้ Class ลูกพึ่งพา Class แม่มากเกินไป (High Coupling) ทำให้ยากต่อการเปลี่ยนแปลงหรือ Re-use Class ในอนาคต

สรุป

protected access modifier เป็นเครื่องมือที่มีประโยชน์ในการออกแบบ Class ที่มีการสืบทอดคุณสมบัติ ช่วยให้ Class ลูกสามารถเข้าถึงและใช้ Properties และ Methods ของ Class แม่ได้ แต่ต้องใช้อย่างระมัดระวังเพื่อไม่ให้ลดระดับ Encapsulation มากเกินไป และเพิ่มความซับซ้อนในการบำรุงรักษาโค้ด

ในบทถัดไป เราจะมาเรียนรู้เกี่ยวกับการใช้ Getter และ Setter methods เพื่อควบคุมการเข้าถึง Properties ของ Object อย่างมีประสิทธิภาพมากขึ้น

Office.in.th Custom Service

この構成をもとにシステムを開発・カスタマイズしたいですか?

Office.in.th は、エンタープライズ Web アプリケーション、API 連携、官民の業務管理システムの設計・開発を承り、長期的なサポートも提供します。

ソフトウェア開発サービスを見る