发新话题
打印

php method_exists判断类成员函数是否存在

php method_exists判断类成员函数是否存在

method_exists

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)));
        }
    }

}
?>

[ 本帖最后由 xiexie 于 2012-7-5 13:22 编辑 ]

TOP

发新话题