函數(shù)名:ReflectionClass::hasMethod()
適用版本:PHP 5 >= 5.1.0, PHP 7
函數(shù)說明:ReflectionClass::hasMethod() 方法用于檢查類是否具有指定的方法。
語法:public bool ReflectionClass::hasMethod ( string $name )
參數(shù):
- $name:要檢查的方法的名稱。
返回值:
- 如果類具有指定的方法,則返回 true,否則返回 false。
示例:
class MyClass {
public function myMethod() {
// ...
}
}
$reflection = new ReflectionClass('MyClass');
// 檢查類是否具有名為 "myMethod" 的方法
if ($reflection->hasMethod('myMethod')) {
echo 'MyClass 類具有 myMethod 方法';
} else {
echo 'MyClass 類不具有 myMethod 方法';
}
輸出結(jié)果:
MyClass 類具有 myMethod 方法
在上面的示例中,我們首先創(chuàng)建了一個名為 MyClass
的類,該類具有一個名為 myMethod
的公共方法。然后,我們使用 ReflectionClass
類創(chuàng)建了一個類的反射實(shí)例 $reflection
。最后,我們使用 $reflection->hasMethod('myMethod')
檢查類是否具有名為 myMethod
的方法,并根據(jù)結(jié)果輸出相應(yīng)的信息。由于 MyClass
類確實(shí)具有 myMethod
方法,因此輸出結(jié)果為 MyClass 類具有 myMethod 方法
。