ReflectionClass::implementsInterface
(PHP 5, PHP 7, PHP 8)
ReflectionClass::implementsInterface — 实现接口
说明
检查其是否实现了接口(interface)。
参数
interface
-
接口(interface)的名称。
错误/异常
如果 interface
不是接口,则
ReflectionClass::implementsInterface() 会抛出
ReflectionException。
参见
- ReflectionClass::isInterface() - 检查类是否是一个接口(interface)
- ReflectionClass::isSubclassOf() - 检查是否为子类
- interface_exists() - 检查接口是否已被定义
- 对象接口
+添加备注
用户贡献的备注 3 notes
jtunaley at gmail dot com ¶
6 years ago
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)
?>
dhairya dot coder at gmail dot com ¶
9 years ago
//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
keepchen2016 at gmail dot com ¶
7 years ago
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
备份地址:http://www.lvesu.com/blog/php/reflectionclass.implementsinterface.php