ReflectionClass::hasMethod()函数是PHP中的内置函数,用于检查指定的方法是否存在。
用法:
bool ReflectionClass::hasMethod( string $name )
参数:该函数接受单个参数$name,该参数保存正在检查的方法的名称。
返回值:如果存在指定的方法,则此函数返回TRUE,否则返回FALSE。
以下示例程序旨在说明PHP中的ReflectionClass::hasMethod()函数:
程序1:
// Initialising a user-defined Class Departments
class Departments {
public function CSE() { }
final protected function ECE() { }
private static function EE() { }
static function IT() { }
private function Mechnical() { }
}
// Using ReflectionClass() over the
// user-defined class Departments
$class = new ReflectionClass('Departments');
// Calling the hasMethod() function
$methods = $class->hasMethod('CSE');
// Getting the value true or false
var_dump($methods);
?>
输出:
bool(true)
程序2:
// Initialising a user-defined Class Company
class Company {
public function GeeksforGeeks() { }
static function gfg() { }
}
// Using ReflectionClass() over the
// user-defined class Company
$class = new ReflectionClass('Company');
// Calling the hasMethod() function
$methods = $class->hasMethod('TCS');
// Getting the value true or false
var_dump($methods);
?>
输出:
bool(false)