PHP对象接口

2023-12-05 120

在PHP中,接口提供了一种机制,允许开发者规定类必须实现的方法集合,但并不强制定义这些方法的具体执行逻辑。接口作为一种契约,确保遵循该接口的类实现了接口中声明的所有方法,它与类、trait位于同一命名空间之下,因此它们之间不能有重名的情况,以避免命名冲突。

接口就像定义一个标准的类一样,通过 interface 关键字替换掉 class 关键字来定义,但其中所有的方法都是空的。接口的特性是定义的所有方法都必须是 public 。

在实践中,往往出于两个辅助目的使用接口:

  • 因为实现了同一个接口,所以开发者创建的对象虽然源自不同的类,但可能可以交换使用。 常用于多个数据库的服务访问、多个支付网关、不同的缓存策略等。 可能不需要任何代码修改,就能切换不同的实现方式;
  • 能够让函数与方法接受一个符合接口的参数,而不需要关心对象如何做、如何实现。 这些接口常常命名成类似 Iterable、Cacheable、Renderable, 以便于体现出功能的含义。

接口可以定义魔术方法,以便要求类(class)实现这些方法。

注意:虽然没有禁止,但是建议不要在接口中使用构造器,因为这样在对象实现接口时,会大幅降低灵活性。此外,也不能强制确保构造器遵守继承规则,将导致不可预料的行为结果。

一、实现

要实现一个接口,使用 implements 操作符。类中必须实现接口中定义的所有方法,否则会报一个致命错误。 类可以实现多个接口,用逗号来分隔多个接口的名称。

注意:实现接口的时候,class 中的参数名称不必和接口完全一致。 然而, PHP 8.0 起语法开始支持命名参数, 也就是说调用方会依赖接口中参数的名称。 因此,强烈建议开发者的参数的命名,在类和接口中保持一致;

接口也可以通过 extends 操作符扩展;

类实现接口时,必须以兼容的签名定义接口中所有方法。类可以实现多个声明了相同方法名称的接口。在这种情况下,实现必须遵循所有接口的签名兼容性规则。因此可以应用协变和逆变。

二、常量

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

三、示例

1、接口示例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?<a href="https://www.zzbaike.com/tag/php" title="查看所有文章关于 php" target="_blank">php</a>
// 声明一个'Template'接口
interface Template
{
public function setVariable($name, $var);
public function getHtml($template);
}
// 实现接口
// 下面的写法是正确的
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;
}
}
// 下面的写法是错误的,会报错,因为没有实现 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;
}
}
?>
<?<a href="https://www.zzbaike.com/tag/php" title="查看所有文章关于 php" target="_blank">php</a> // 声明一个'Template'接口 interface Template { public function setVariable($name, $var); public function getHtml($template); } // 实现接口 // 下面的写法是正确的 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; } } // 下面的写法是错误的,会报错,因为没有实现 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; } } ?>
<?php

// 声明一个'Template'接口
interface Template
{
    public function setVariable($name, $var);
    public function getHtml($template);
}


// 实现接口
// 下面的写法是正确的
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;
    }
}

// 下面的写法是错误的,会报错,因为没有实现 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、可扩充的接口

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
interface A
{
public function foo();
}
interface B extends A
{
public function baz(Baz $baz);
}
// 正确写法
class C implements B
{
public function foo()
{
}
public function baz(Baz $baz)
{
}
}
// 错误写法会导致一个致命错误
class D implements B
{
public function foo()
{
}
public function baz(Foo $foo)
{
}
}
?>
<?php interface A { public function foo(); } interface B extends A { public function baz(Baz $baz); } // 正确写法 class C implements B { public function foo() { } public function baz(Baz $baz) { } } // 错误写法会导致一个致命错误 class D implements B { public function foo() { } public function baz(Foo $foo) { } } ?>
<?php
interface A
{
    public function foo();
}

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

// 正确写法
class C implements B
{
    public function foo()
    {
    }

    public function baz(Baz $baz)
    {
    }
}

// 错误写法会导致一个致命错误
class D implements B
{
    public function foo()
    {
    }

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

3、多接口的差异兼容性

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
class Foo {}
class Bar extends Foo {}
interface A {
public function myfunc(Foo $arg): Foo;
}
interface B {
public function myfunc(Bar $arg): Bar;
}
class MyClass implements A, B
{
public function myfunc(Foo $arg): Bar
{
return new Bar();
}
}
?>
<?php class Foo {} class Bar extends Foo {} interface A { public function myfunc(Foo $arg): Foo; } interface B { public function myfunc(Bar $arg): Bar; } class MyClass implements A, B { public function myfunc(Foo $arg): Bar { return new Bar(); } } ?>
<?php
class Foo {}
class Bar extends Foo {}

interface A {
    public function myfunc(Foo $arg): Foo;
}

interface B {
    public function myfunc(Bar $arg): Bar;
}

class MyClass implements A, B
{
    public function myfunc(Foo $arg): Bar
    {
        return new Bar();
    }
}
?>

4、扩展多个接口

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
interface A
{
public function foo();
}
interface B
{
public function bar();
}
interface C extends A, B
{
public function baz();
}
class D implements C
{
public function foo()
{
}
public function bar()
{
}
public function baz()
{
}
}
?>
<?php interface A { public function foo(); } interface B { public function bar(); } interface C extends A, B { public function baz(); } class D implements C { public function foo() { } public function bar() { } public function baz() { } } ?>
<?php
interface A
{
    public function foo();
}

interface B
{
    public function bar();
}

interface C extends A, B
{
    public function baz();
}

class D implements C
{
    public function foo()
    {
    }

    public function bar()
    {
    }

    public function baz()
    {
    }
}
?>

5、使用接口常量

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
interface A
{
const B = 'Interface constant';
}
// 输出接口常量
echo A::B;
// 错误写法,因为常量不能被覆盖。接口常量的概念和类常量是一样的。
class B implements A
{
const B = 'Class constant';
}
// 输出: Class constant
// 在 PHP 8.1.0 之前,不能正常运行
// 因为之前还不允许覆盖类常量。
echo B::B;
?>
<?php interface A { const B = 'Interface constant'; } // 输出接口常量 echo A::B; // 错误写法,因为常量不能被覆盖。接口常量的概念和类常量是一样的。 class B implements A { const B = 'Class constant'; } // 输出: Class constant // 在 PHP 8.1.0 之前,不能正常运行 // 因为之前还不允许覆盖类常量。 echo B::B; ?>
<?php
interface A
{
    const B = 'Interface constant';
}

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

// 错误写法,因为常量不能被覆盖。接口常量的概念和类常量是一样的。
class B implements A
{
    const B = 'Class constant';
}

// 输出: Class constant
// 在 PHP 8.1.0 之前,不能正常运行
// 因为之前还不允许覆盖类常量。
echo B::B;
?>

6、抽象类的接口使用

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
interface A
{
public function foo(string $s): string;
public function bar(int $i): int;
}
// 抽象类可能仅实现了接口的一部分。
// 扩展该抽象类时必须实现剩余部分。
abstract class B implements A
{
public function foo(string $s): string
{
return $s . PHP_EOL;
}
}
class C extends B
{
public function bar(int $i): int
{
return $i * 2;
}
}
?>
<?php interface A { public function foo(string $s): string; public function bar(int $i): int; } // 抽象类可能仅实现了接口的一部分。 // 扩展该抽象类时必须实现剩余部分。 abstract class B implements A { public function foo(string $s): string { return $s . PHP_EOL; } } class C extends B { public function bar(int $i): int { return $i * 2; } } ?>
<?php
interface A
{
    public function foo(string $s): string;

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

// 抽象类可能仅实现了接口的一部分。
// 扩展该抽象类时必须实现剩余部分。
abstract class B implements A
{
    public function foo(string $s): string
    {
        return $s . PHP_EOL;
    }
}

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

7、同时使用扩展和实现

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
class One
{
/* ... */
}
interface Usable
{
/* ... */
}
interface Updatable
{
/* ... */
}
// 关键词顺序至关重要: 'extends' 必须在前面
class Two extends One implements Usable, Updatable
{
/* ... */
}
?>
<?php class One { /* ... */ } interface Usable { /* ... */ } interface Updatable { /* ... */ } // 关键词顺序至关重要: 'extends' 必须在前面 class Two extends One implements Usable, Updatable { /* ... */ } ?>
<?php

class One
{
    /* ... */
}

interface Usable
{
    /* ... */
}

interface Updatable
{
    /* ... */
}

// 关键词顺序至关重要: 'extends' 必须在前面
class Two extends One implements Usable, Updatable
{
    /* ... */
}
?>
  • 广告合作

  • QQ群号:4114653

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。
PHP对象接口
上一篇: PHP抽象类