如何使用对象方法作为回调函数 [英] How to use an object method as a callback function

查看:256
本文介绍了如何使用对象方法作为回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单例课程中有以下方法

I have the below method in a singleton class

private function encode($inp)
{
    if (is_array($inp) {
        return array_map('$this->encode', $inp);
    } else if is_scalar($inp) {
        return str_replace('%7E', rawurlencode($inp));
    } else {
        return '';
    }
}

这可以正常使用

function encode($inp)
{
    if (is_array($inp) {
        return array_map('encode', $inp);
    } else if is_scalar($inp) {
        return str_replace('%7E', rawurlencode($inp));
    } else {
        return '';
    }
}

在类中使用时,出现以下错误:

when using inside a class i'm getting the below error:

PHP警告:array_map():第一个 参数"$ this-> rfc_encode"应 为NULL或有效的回调

PHP Warning: array_map(): The first argument, '$this->rfc_encode', should be either NULL or a valid callback

请有人帮我解决这个问题.

Please could anybody help me to fix this.

推荐答案

来自

实例化对象的方法作为数组传递,该数组包含索引为0的对象和索引为1的方法名称.

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

所以尝试

return array_map(array($this, 'encode'), $inp);

这篇关于如何使用对象方法作为回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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