函数名:ReflectionClass::hasMethod()
适用版本:PHP 5 >= 5.1.0, PHP 7
函数说明:ReflectionClass::hasMethod() 方法用于检查类是否具有指定的方法。
语法:public bool ReflectionClass::hasMethod ( string $name )
参数:
- $name:要检查的方法的名称。
返回值:
- 如果类具有指定的方法,则返回 true,否则返回 false。
示例:
class MyClass {
public function myMethod() {
// ...
}
}
$reflection = new ReflectionClass('MyClass');
// 检查类是否具有名为 "myMethod" 的方法
if ($reflection->hasMethod('myMethod')) {
echo 'MyClass 类具有 myMethod 方法';
} else {
echo 'MyClass 类不具有 myMethod 方法';
}
输出结果:
MyClass 类具有 myMethod 方法
在上面的示例中,我们首先创建了一个名为 MyClass
的类,该类具有一个名为 myMethod
的公共方法。然后,我们使用 ReflectionClass
类创建了一个类的反射实例 $reflection
。最后,我们使用 $reflection->hasMethod('myMethod')
检查类是否具有名为 myMethod
的方法,并根据结果输出相应的信息。由于 MyClass
类确实具有 myMethod
方法,因此输出结果为 MyClass 类具有 myMethod 方法
。