最新消息: 新版网站上线了!!!

call_user_func_array:传递类名或者对象Non-static method cases::create() should not be

前提:
class cases
{
    public function create($id){
        echo '这里定义了一个类cases,当前方法为create';
    }
}


一、传递类名:
call_user_func_array(array($className, $methodName), $params);
call_user_func_array(array('cases', 'create'), 1);
提示错误:Non-static method cases::create() should not be called statically in
原因:如果这个参数直接是类名称$className,那么传递的这个$methodName方法名称,对应的方法必须是静态方法,所以修改为如下格式就不会报错了
public static  function create($id)

二、传递对象
call_user_func_array(array($objectName, $methodName), $params);
解答:其实针对上面的错误,还有一个修改办法就是,对上面的类cases实例化对象,然后传递对象给参数$objectName,此时即使create方法非静态也不会提示错误
比如:
$cases = new cases();
call_user_func_array(array($cases, 'create'), 1);

转载请注明:谷谷点程序 » call_user_func_array:传递类名或者对象Non-static method cases::create() should not be