Call non static method dynamically

只愿长相守 提交于 2020-01-25 21:05:19

问题


i tried to call non static method like that:

call_user_func_array(array("Notifications", "getNots"), $params)

and i got error: call_user_func() expects parameter 1 to be a valid callback, non-static method...

the function is:

class Notifications {
    public function getNots($limit, $test = 0) {
    }
}

what to do?

actually i tried to built function that got html code of html page and replace all texts like that:

{{ Notifications.getNotes(3) }}

to method return...

tnx a lot


回答1:


To do this with a non-static function, you need to instantiate a Notification object (if not alrady done, then pass it into call_user_func_array() as the first value in the callback array. Something like this:

$notifications = new Notification();
call_user_func_array(array($notifications, "getNots"), $params);

If already instantiated, you pass the instance, rather than the class name.



来源:https://stackoverflow.com/questions/33201004/call-non-static-method-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!