加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何使用对象方法作为回调函数

发布时间:2020-12-13 14:09:46 所属栏目:PHP教程 来源:网络整理
导读:我在单身人类中有以下方法 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 ''; }} 这作为一个普通的功能工作正
我在单身人类中有以下方法
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',rawurlencode($inp));
    } else {
        return '';
    }
}

当在类里面使用我得到以下错误:

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

请任何人帮我解决这个问题.

从 PHP Manual on Callbacks:

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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读