PHP Conference Japan 2024

ReflectionClass::implementsInterface

(PHP 5, PHP 7, PHP 8)

ReflectionClass::implementsInterface實作介面

說明

public ReflectionClass::implementsInterface(ReflectionClass|string $interface): bool

檢查是否實作了介面。

參數

interface

介面名稱。

傳回值

成功時傳回 true,失敗時傳回 false

錯誤/例外

如果 interface 不是介面,ReflectionClass::implementsInterface() 會丟出 ReflectionException 例外。

另請參閱

新增註解

使用者貢獻的註解 3 則註解

jtunaley at gmail dot com
6 年前
請注意,當您反射的物件是您正在檢查的介面時,此方法也會返回 true

<?php
interface MyInterface {}

$reflect = new ReflectionClass('MyInterface');
var_dump($reflect->implementsInterface('MyInterface')); // bool(true)
?>
dhairya dot coder at gmail dot com
8 年前
// 檢查 Fruit 類別是否實作了 Apple 介面

interface Apple {

function taste();
}

class Fruit implements Apple {

function taste() {
echo "Seet";
}
}

$obj=new ReflectionClass('Fruit');
var_dump($obj->implementsInterface('Apple')); // 這裡會檢查 Fruit 類別是否實作了 Apple 介面
keepchen2016 at gmail dot com
7 年前
interface Factory {
{
public function sayHello();
}

class ParentClass implements Factory {
{
public function sayHello() {
{
}
}
}

class ChildrenClass extends ParentClass {}
{

}

$reflect = new ReflectionClass('ParentClass');
var_dump($reflect->implementsInterface('Factory'));

$second_ref = new ReflectionClass('ChildrenClass');
var_dump($second_ref->isSubclassOf('ParentClass'));

$third_ref = new ReflectionClass('Factory');
var_dump($third_ref->isInterface());

// 不能以靜態方式呼叫
var_dump(ReflectionClass::isInterface('Factory'));
die;
//#結果
bool(true)
bool(true)
bool(true)
PHP 致命錯誤:無法以靜態方式呼叫非靜態方法 ReflectionClass::isInterface()
To Top