對(duì)象接口

使用接口(interface),可以指定某個(gè)類必須實(shí)現(xiàn)哪些方法,但不需要定義這些方法的具體內(nèi)容。 由于接口(interface)和類(class)、trait 共享了命名空間,所以它們不能重名。

接口就像定義一個(gè)標(biāo)準(zhǔn)的類一樣,通過 interface 關(guān)鍵字替換掉 class 關(guān)鍵字來定義,但其中所有的方法都是空的。

接口中定義的所有方法都必須是 public ,這是接口的特性。

在實(shí)踐中,往往出于兩個(gè)輔助目的使用接口:

  • 因?yàn)閷?shí)現(xiàn)了同一個(gè)接口,所以開發(fā)者創(chuàng)建的對(duì)象雖然源自不同的類,但可能可以交換使用。 常用于多個(gè)數(shù)據(jù)庫的服務(wù)訪問、多個(gè)支付網(wǎng)關(guān)、不同的緩存策略等。 可能不需要任何代碼修改,就能切換不同的實(shí)現(xiàn)方式。
  • 能夠讓函數(shù)與方法接受一個(gè)符合接口的參數(shù),而不需要關(guān)心對(duì)象如何做、如何實(shí)現(xiàn)。 這些接口常常命名成類似 Iterable、CacheableRenderable, 以便于體現(xiàn)出功能的含義。

接口可以定義魔術(shù)方法,以便要求類(class)實(shí)現(xiàn)這些方法。

注意:

雖然沒有禁止,但是強(qiáng)烈建議不要在接口中使用 構(gòu)造器。 因?yàn)檫@樣在對(duì)象實(shí)現(xiàn)接口時(shí),會(huì)大幅降低靈活性。 此外,也不能強(qiáng)制確保構(gòu)造器遵守繼承規(guī)則,將導(dǎo)致不可預(yù)料的行為結(jié)果。

實(shí)現(xiàn)(implements

要實(shí)現(xiàn)一個(gè)接口,使用 implements 操作符。類中必須實(shí)現(xiàn)接口中定義的所有方法,否則會(huì)報(bào)一個(gè)致命錯(cuò)誤。 類可以實(shí)現(xiàn)多個(gè)接口,用逗號(hào)來分隔多個(gè)接口的名稱。

警告

類實(shí)現(xiàn)(implement)兩個(gè)接口時(shí),如果它們定義了相同名稱的方法,只有簽名相同的時(shí)候才是允許的。

警告

實(shí)現(xiàn)接口的時(shí)候,class 中的參數(shù)名稱不必和接口完全一致。 然而, PHP 8.0 起語法開始支持命名參數(shù), 也就是說調(diào)用方會(huì)依賴接口中參數(shù)的名稱。 因此,強(qiáng)烈建議開發(fā)者的參數(shù)的命名,在類和接口中保持一致。

注意:

接口也可以通過 extends 操作符擴(kuò)展。

注意:

類實(shí)現(xiàn)接口時(shí),必須以兼容的簽名定義接口中所有方法。

常量

接口中也可以定義常量。接口常量和類常量的使用完全相同, 在 PHP 8.1.0 之前 不能被子類或子接口所覆蓋。

范例

示例 #1 接口示例

<?php

// 聲明一個(gè)'Template'接口
interface Template
{
    public function 
setVariable($name$var);
    public function 
getHtml($template);
}


// 實(shí)現(xiàn)接口
// 下面的寫法是正確的
class WorkingTemplate implements Template
{
    private 
$vars = [];
  
    public function 
setVariable($name$var)
    {
        
$this->vars[$name] = $var;
    }
  
    public function 
getHtml($template)
    {
        foreach(
$this->vars as $name => $value) {
            
$template str_replace('{' $name '}'$value$template);
        }
 
        return 
$template;
    }
}

// 下面的寫法是錯(cuò)誤的,會(huì)報(bào)錯(cuò),因?yàn)闆]有實(shí)現(xiàn) getHtml():
// Fatal error: Class BadTemplate contains 1 abstract methods
// and must therefore be declared abstract (Template::getHtml)
class BadTemplate implements Template
{
    private 
$vars = [];
  
    public function 
setVariable($name$var)
    {
        
$this->vars[$name] = $var;
    }
}
?>

示例 #2 可擴(kuò)充的接口

<?php
interface A
{
    public function 
foo();
}

interface 
extends A
{
    public function 
baz(Baz $baz);
}

// 正確寫法
class implements B
{
    public function 
foo()
    {
    }

    public function 
baz(Baz $baz)
    {
    }
}

// 錯(cuò)誤寫法會(huì)導(dǎo)致一個(gè)致命錯(cuò)誤
class implements B
{
    public function 
foo()
    {
    }

    public function 
baz(Foo $foo)
    {
    }
}
?>

示例 #3 擴(kuò)展多個(gè)接口

<?php
interface A
{
    public function 
foo();
}

interface 
B
{
    public function 
bar();
}

interface 
extends AB
{
    public function 
baz();
}

class 
implements C
{
    public function 
foo()
    {
    }

    public function 
bar()
    {
    }

    public function 
baz()
    {
    }
}
?>

示例 #4 使用接口常量

<?php
interface A
{
    const 
'Interface constant';
}

// 輸出接口常量
echo A::B;

// 錯(cuò)誤寫法,因?yàn)槌A坎荒鼙桓采w。接口常量的概念和類常量是一樣的。
class implements A
{
    const 
'Class constant';
}

// 輸出: Class constant
// 在 PHP 8.1.0 之前,不能正常運(yùn)行
// 因?yàn)橹斑€不允許覆蓋類常量。
echo B::B;
?>

示例 #5 抽象(abstract)類的接口使用

<?php
interface A
{
    public function 
foo(string $s): string;

    public function 
bar(int $i): int;
}

// 抽象類可能僅實(shí)現(xiàn)了接口的一部分。
// 擴(kuò)展該抽象類時(shí)必須實(shí)現(xiàn)剩余部分。
abstract class implements A
{
    public function 
foo(string $s): string
    
{
        return 
$s PHP_EOL;
    }
}

class 
extends B
{
    public function 
bar(int $i): int
    
{
        return 
$i 2;
    }
}
?>

示例 #6 同時(shí)使用擴(kuò)展和實(shí)現(xiàn)

<?php

class One
{
    
/* ... */
}

interface 
Usable
{
    
/* ... */
}

interface 
Updatable
{
    
/* ... */
}

// 關(guān)鍵詞順序至關(guān)重要: 'extends' 必須在前面
class Two extends One implements UsableUpdatable
{
    
/* ... */
}
?>

接口加上類型約束,提供了一種很好的方式來確保某個(gè)對(duì)象包含有某些方法。參見 instanceof 操作符和類型聲明。