Interfaces ในภาษา PHP
ในบทนี้ คุณจะได้เรียนรู้เกี่ยวกับ Interfaces ในภาษา PHP ในการเขียนโปรแกรมเชิงวัตถุ (OOP) Interfaces คือประเภทข้อมูลแบบนามธรรม (Abstract type) ที่ไม่ได้มีการกำหนดรายละเอียดหรือการทำงานที่แน่นอน อาจจะเป็นคลาสหรือเมธอดที่ไม่มีการทำงานอยู่ข้างใน ซึ่งแตกต่างจาก Interfaces ของฮาร์ดแวร์ในคอมพิวเตอร์
Interfaces คืออะไร
ในการเขียนโปรแกรมเชิงวัตถุ Interfaces คือการกำหนดประเภทข้อมูลแบบนามธรรมที่ไม่มีข้อมูลหรือโค้ดการทำงานภายใน และกำหนดเมธอดที่มีเพียงส่วนหัวของเมธอด (Method signature) และ Interfaces จะถูกนำไปกำหนดการทำงานโดยคลาส ซึ่งคลาสที่มีโค้ดและข้อมูลที่ประกอบไปด้วยเมธอดที่สอดคล้องกับ Interfaces เรียกว่าการ Implement interfaces และนอกจากนี้ คลาสสามารถ Implement ได้หลาย Interfaces ในเวลาเดียวกัน
ในรูปเป็นการแสดงแนวคิดของ Interfaces ซึ่งโทรศัพท์และคอมพิวเตอร์นั้นอาจจะมีการทำงานบางอย่างทีเหมือนกัน เช่น การเปิดปิดเครื่อง การเพิ่มลดเสียง แต่การทำงานของมันอาจจะต่างกัน Interfaces จึงบอกว่าอะไรบ้างที่ออบเจ็คสามารถทำได้ แต่ไม่ได้บอกว่ามันมีการทำอย่างไร
Interfaces นั้นมีลักษณะเหมือนคลาส แต่สิ่งที่แตกต่างกันระหว่างคลาสกับ Interfaces คือ Interfaces จะไม่สามารถนำไปสร้างออบเจ็คได้ ดังนั้นเพื่อนำเอา Interfaces ไปใช้งาน จึงต้องสร้างคลาสเพื่อที่จะ Implements การทำงานของ Interfaces
การสร้างและใช้งาน Interfaces
ในตัวอย่างเราจะสร้าง Interfaces สำหรับรวบรวมการทำงานของโทรทัศน์และวิทยุ และเราจะมีคลาสสำหรับเครื่องใช้ไฟฟ้าทั้งสองประเภทนี้ ที่จะทำการ Implement interfaces ดังกล่าว
interface iTelevision {
public function turnOn();
public function turnOff();
public function changeChannel($newChannel);
}
ในตัวอย่างเราได้สร้าง Interfaces iTelevision
ซึ่งใช้คำสั่ง interface
สำหรับสร้าง Interfaces ในภาษา PHP ภายใน Interfaces เราได้มีการประกาศเมธอดสามเมธอด ซึ่งเมธอดเหล่านี้จะไม่มีการกำหนดโค้ดการทำงาน และมันจะถูกนำไปใช้โดยการ Implement โดยคลาส
class Television implements iTelevision {
public $currentChannel = 1;
public function turnOn() {
echo "Television has turned on\n";
}
public function turnOff() {
echo "Television has turned off\n";
}
public function changeChannel($newChannel) {
echo "Changed channel from ";
echo "$this->currentChannel to $newChannel\n";
$this->currentChannel = $newChannel;
}
}
ต่อมาไปเป็นการสร้างคลาสในการ Implement interfaces โดยใช้คำสั่ง implements
หลังจากชื่่อคลาสและตามด้วยชื่อของ Interfaces iTelevision
ภายในคลาส Television
จะต้องทำการกำหนดการทำงานทุกเมธอดที่ได้กำหนดใน Interfaces iTelevision
class Radio implements iTelevision {
public $currentChannel = 1;
public function turnOn() {
echo "Radio has turned on\n";
}
public function turnOff() {
echo "Radio has turned off\n";
}
public function changeChannel($newChannel) {
echo "Changed channel from ";
echo "$this->currentChannel to $newChannel\n";
$this->currentChannel = $newChannel;
}
}
ในขณะเดียวกัน เราได้สร้างคลาส Radio
ซึ่งก็ทำการ Implement interfaces iTelevision
เพราะว่าวิทยุนั้นมีการทำงานทีเ่หมือนกันคือ การเปิด การปิด หรือการเปลี่ยนช่อง ดังนั้น Interfaces หนึ่งสามารถนำไป Implement ได้หลายคลาส
Implementing multiple-interfaces
ในการสร้างคลาสในภาษา PHP นั้นเป็นไปได้ที่คุณอาจจะต้องการ Implement หลาย Interfaces ได้ในครั้งเดียวกัน ยกตัวอย่าง เช่น Interfaces แต่ละอันอาจจะกำหนดกลุ่มของฟังก์ชันที่แตกต่างกันออกไป มาดูตัวอย่าการ Implement หลาย Interfaces ในภาษา PHP
<?php
interface iTelevision {
public function turnOn();
public function turnOff();
public function changeChannel($newChannel);
}
interface iVolume {
public function volumeUp();
public function volumeDown();
}
class Television implements iTelevision, iVolume {
public $currentChannel = 1;
public $currentVolume = 50;
// Implementing iTelevision interface
public function turnOn() {
echo "Television has turned on\n";
}
public function turnOff() {
echo "Television has turned off\n";
}
public function changeChannel($newChannel) {
echo "Changed channel from ";
echo "$this->currentChannel to $newChannel\n";
$this->currentChannel = $newChannel;
}
// Implementing iVolume interface
public function volumeUp() {
$this->currentVolume += 5;
echo "Volume up to $this->currentVolume%\n";
}
public function volumeDown() {
$this->currentVolume -= 5;
echo "Volume down to $this->currentVolume%\n";
}
// Methods in class
public function addToFavoriteChannel() {
echo "Channel $this->currentChannel added to";
echo " favorite channel \n";
}
}
$tv = new Television();
$tv->turnOn();
$tv->volumeUp();
$tv->volumeUp();
$tv->changeChannel(34);
$tv->changeChannel(10);
$tv->addToFavoriteChannel();
$tv->turnOff();
?>
ในตัวอย่างเป็นโปรแกรมการใช้งาน Interfaces ที่พร้อมทำงาน เราได้สร้าง iVolume
เพิ่มเข้ามาซึ่งเป็น Interfaces ในหารกำหนดเมธอดเกี่ยวกับการควบคุมระดับเสียง และในคลาส Television
เราต้องทำการกำหนดการทำงานให้กับเมธอดทั้งหมดจาก Interfaces ทั้งสอง และคลาสยังสามารถมีตัวแปรและเมธอดของมันเองได้ตามปกติ
Television has turned on
Volume up to 55%
Volume up to 60%
Changed channel from 1 to 34
Changed channel from 34 to 10
Chanel 10 added to favorite channel
Television has turned off
นี่เป็นผลลลัพธ์ของโปรแกรมในการใช้งาน Interfaces ในภาษา PHP
ในบทนี้ คุณได้เรียนรู้เกี่ยวกับความหมายและการใช้งาน Interfaces ในการเขียนโปรแกรมเชิงวัตถุในภาษา PHP