แนะนำตัว
อินเทอร์เฟซเป็นคุณลักษณะที่สำคัญของการเขียนโปรแกรมเชิงวัตถุ โดยสามารถระบุวิธีการที่จะนำไปใช้โดยชั้นเรียน โดยไม่ต้องกำหนดว่าควรใช้งานอย่างไร
PHP รองรับอินเทอร์เฟซโดยวิธีถ้า อินเทอร์เฟซ คำสำคัญ. อินเทอร์เฟซคล้ายกับคลาส แต่มีเมธอดที่ไม่มีเนื้อหาคำจำกัดความ วิธีการในส่วนต่อประสานจะต้องเป็นแบบสาธารณะ คลาสที่สืบทอดมาซึ่งใช้วิธีการเหล่านี้ต้องกำหนดด้วย การใช้งาน คีย์เวิร์ดแทนที่จะขยายคีย์เวิร์ด และต้องจัดเตรียมการใช้งานเมธอดทั้งหมดในอินเทอร์เฟซหลัก
ไวยากรณ์
<?php
interface testinterface{
public function testmethod();
}
class testclass implements testinterface{
public function testmethod(){
echo "implements interfce method";
}
}
?> เมธอดทั้งหมดจากอินเทอร์เฟซต้องถูกกำหนดโดยคลาสการนำ มิฉะนั้น PHP parser จะส่งข้อยกเว้น
ตัวอย่าง
<?php
interface testinterface{
public function test1();
public function test2();
}
class testclass implements testinterface{
public function test1(){
echo "implements interface method";
}
}
$obj=new testclass()
?> ผลลัพธ์
ข้อผิดพลาดดังแสดงด้านล่าง −
PHP Fatal error: Class testclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (testinterface::test2)
อินเทอร์เฟซที่ขยายได้
เช่นเดียวกับคลาสปกติ อินเทอร์เฟซยังสามารถสืบทอดโดยใช้ ขยาย คำสำคัญ
ในตัวอย่างต่อไปนี้ คลาสพาเรนต์มีเมธอดนามธรรมสองเมธอด ซึ่งมีเพียงหนึ่งเมธอดที่กำหนดใหม่ในคลาสย่อย ซึ่งส่งผลให้เกิดข้อผิดพลาดดังต่อไปนี้ -
ตัวอย่าง
<?php
interface testinterface{
public function test1();
}
interface myinterface extends testinterface{
public function test2();
}
class testclass implements myinterface{
public function test1(){
echo "implements test1 method";
}
public function test2(){
echo "implements test2 method";
}
}
?> การสืบทอดหลายรายการโดยใช้อินเทอร์เฟซ
PHP ไม่อนุญาตให้มีมากกว่าหนึ่งคลาสในส่วนคำสั่งขยาย อย่างไรก็ตาม การสืบทอดหลายรายการสามารถทำได้โดยให้คลาสลูกใช้อินเทอร์เฟซอย่างน้อยหนึ่งอินเทอร์เฟซ
ในตัวอย่างต่อไปนี้ myclass จะขยาย testclass และใช้ testinterface เพื่อให้ได้รับมรดกหลายรายการ
ตัวอย่าง
<?php
interface testinterface{
public function test1();
}
class testclass{
public function test2(){
echo "this is test2 function in parent class\n";
}
}
class myclass extends testclass implements testinterface{
public function test1(){
echo "implements test1 method\n";
}
}
$obj=new myclass();
$obj->test1();
$obj->test2();
?> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
implements test1 method this is test2 function in parent class
ตัวอย่างอินเทอร์เฟซ
ตัวอย่าง
<?php
interface shape{
public function area();
}
class circle implements shape{
private $rad;
public function __construct(){
$this->rad=5;
}
public function area(){
echo "area of circle=" . M_PI*pow($this->rad,2) ."\n";
}
}
class rectangle implements shape{
private $width;
private $height;
public function __construct(){
$this->width=20;
$this->height=10;
}
public function area(){
echo "area of rectangle=" . $this->width*$this->height ."\n";
}
}
$c=new circle();
$c->area();
$r=new rectangle();
$r->area();
?> ผลลัพธ์
สคริปต์ด้านบนสร้างผลลัพธ์ต่อไปนี้
area of circle=78.539816339745 area of rectangle=200