动态调用非静态方法 [英] Call non static method dynamically

查看:97
本文介绍了动态调用非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用这样的非静态方法:

i tried to call non static method like that:

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

而我得到了错误:
call_user_func()期望参数1为a有效的回调,非静态方法...

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

函数为:

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

该怎么办?

实际上,我尝试构建具有html页面
的html代码的函数并替换所有类似的文本:

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

{{ Notifications.getNotes(3) }}

返回方法...

tnx很多

推荐答案

要使用非静态功能执行此操作,您需要实例化一个Notification对象(如果尚未完成,则首先将其传递到call_user_func_array()中)值在回调数组中。类似这样:

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.

这篇关于动态调用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆