Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

อินเทอร์เฟซ (Interface) ใน PHP

บทนำ

Interface เป็นคุณสมบัติสำคัญของการเขียนโปรแกรมเชิงวัตถุ ที่ช่วยระบุว่าคลาสใดคลาสหนึ่งต้อง Implement เมธอดใดบ้าง โดยไม่ต้องกำหนดรายละเอียดการทำงาน (Implementation) PHP รองรับ Interface ผ่านคีย์เวิร์ด interface

Interface คล้ายกับคลาสแตกต่างที่:

  • มีแต่การประกาศเมธอด (Method Signature) ไม่มี Body/Implementation
  • เมธอดทั้งหมดต้องเป็น public
  • คลาสที่ Implement ต้องใช้คีย์เวิร์ด implements (แทน extends)
  • ต้อง Implement เมธอดทั้งหมดที่ Interface กำหนด

Syntax พื้นฐาน

<?php
interface testinterface {
    public function testmethod();
}

class testclass implements testinterface {
    public function testmethod() {
        echo "implements interface method";
    }
}
?>

ตัวอย่าง: คลาสไม่ Implement เมธอดครบ

ถ้าคลาส Implement Interface แต่ไม่ครบเมธอดทั้งหมด จะเกิด Fatal Error:

<?php
interface testinterface {
    public function test1();
    public function test2();
}

class testclass implements testinterface {
    public function test1() {
        echo "implements interface method";
    }
    // ไม่มี test2() - จะ Error
}
$obj = new testclass();
?>

ผลลัพธ์ (Error)

PHP Fatal error: Class testclass contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (testinterface::test2)

Interface สามารถ Extends ได้

Interface สามารถสืบทอดมาจาก Interface อื่นได้ด้วยคีย์เวิร์ด extends:

<?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";
    }
}
?>

Multiple Inheritance ผ่าน Interface

PHP ไม่รองรับ Multiple Inheritance สำหรับ Class (extends ได้แค่ 1 คลาส) แต่สามารถ Implement หลาย Interfaces ได้:

<?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

ตัวอย่างจริง: Interface Shape

<?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

สรุปข้อดีของ Interface

  • บังคับโครงสร้าง (Contract) ให้คลาสปฏิบัติตาม
  • รองรับ Multiple Implementation (Implements หลาย Interface)
  • ช่วย Polymorphism - ใช้ Interface Type เป็น Type Hint ได้
  • แยก Definition ออกจาก Implementation