php教程

ReflectionClass::implementsInterface

(PHP 5, PHP 7)

ReflectionClass::implementsInterface接口的实现

说明

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

检查它是否实现了一个接口(interface)。

参数

interface

接口(interface)的名称。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

User Contributed Notes

jtunaley at gmail dot com 06-Jun-2018 10:22
Note that this method also returns true when the thing you're reflecting is the interface you're checking for:

<?php
interface MyInterface {}

$reflect = new ReflectionClass('MyInterface');
var_dump($reflect->implementsInterface('MyInterface')); // bool(true)
?>
keepchen2016 at gmail dot com 02-Jun-2017 08:49
interface Factory
{
    public function sayHello();
}

class ParentClass implements Factory
{
    public function sayHello()
    {
        echo "hello\n";
    }
}

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());

//can not be called as static
var_dump(ReflectionClass::isInterface('Factory'));
die;
//#result
bool(true)
bool(true)
bool(true)
PHP Fatal error:  Non-static method ReflectionClass::isInterface() cannot be called statically
dhairya dot coder at gmail dot com 11-Feb-2016 03:48
//checks that whether class Fruit implements interface apple or not

interface Apple {
   
    function taste();
}

class Fruit implements Apple {
   
    function taste() {
        echo "Seet";
    }
}

$obj=new ReflectionClass('Fruit');
var_dump($obj->implementsInterface('Apple'));  //Here it will checks that whether class Fruit implements interface apple or not

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788