bool method_exists
( mixed $object
, string $method_name
) 判断方法是否存在于类中
object 实例化对象或者类名
method_name 方法名
Returns TRUE
if the method given by method_name
has been defined for the given object
, FALSE
otherwise.
例子:
<?php
$directory = new Directory('.');
var_dump(method_exists($directory,'read'));
?>
<?php
class Something
{
/**
* Call a method dynamically
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
if(method_exists($this, $method)) {
return call_user_func_array(array($this, $method), $args);
}else{
throw new Exception(sprintf('The required method "%s" does not exist for %s', $method, get_class($this)));
}
}
}
?>
欢迎光临 PHP开发笔记 (http://phpvi.com/) | Powered by Discuz! 6.1.0 |