Call non static method dynamically

2019-09-11 01:10发布

问题:

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.