牛牛在线工具

ReflectionClass::implementsInterface()

收录 8868 个 PHP 函数,含用途说明与用法示例。

ReflectionClass::implementsInterface()

检查一个类是否实现了指定的接口

函数名:ReflectionClass::implementsInterface() 适用版本:PHP 5 >= 5.1.0, PHP 7 函数描述:该方法用于检查一个类是否实现了指定的接口。 语法:bool ReflectionClass::implementsInterface ( mixed $interface ) 参数: $interface:要检查的接口,可以是接口名称的字符串,也可以是一个 ReflectionClass 对象。 返回值: 如果类实现了指定的接口,则返回 true,否则返回 false。 示例: // 定义一个接口 interface MyInterface { public function myMethod(); } // 定义一个类实现该接口 class MyClass implements MyInterface { public function myMethod() { echo "MyClass 实现了 MyInterface 接口的 myMethod() 方法"; } } // 创建 ReflectionClass 对象 $reflectionClass = new ReflectionClass('MyClass'); // 检查类是否实现了指定的接口 if ($reflectionClass->implementsInterface('MyInterface')) { echo "MyClass 实现了 MyInterface 接口"; } else { echo "MyClass 未实现 MyInterface 接口"; } // 输出结果:MyClass 实现了 MyInterface 接口 在上面的示例中,我们定义了一个接口 MyInterface,然后定义了一个类 MyClass,该类实现了该接口的方法 myMethod()。然后,我们使用 ReflectionClass 类创建了一个 MyClass 的反射类对象,并使用 implementsInterface() 方法来检查该类是否实现了 MyInterface 接口。由于 MyClass 确实实现了该接口,所以输出结果为 "MyClass 实现了 MyInterface 接口"。