tags:

views:

38

answers:

2

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 '';
    }
}

this works fine as an ordinary function

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 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.

+2  A: 

From 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.

So try

return array_map(array($this, 'encode'), $inp);
Gordon
Thanks buddy, it worked.
Kaartz
A: 

Release the single code from $this->encode.

> 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 '';
>     } }

Hope that clears the issue.

Fero
`$this->encode` has no value. There are no real function pointers in PHP, so this is not valid PHP for a callback. It'd only work if you had something like `$this->encode = array(`, which is just a hack to make this specific code work.
cHao
thanks for the clearance cHao. Helps me a lot
Fero
Thanks a lot for your help Chao and Fero.
Kaartz