views:

225

answers:

2

I have this code in one of my classes

 public function __call($method, $args) {

        array_unshift($args, $method);

        call_user_method_array('view', $this, $args);

    }

We've since switched servers, and they must use a newer version of PHP5, and I get the following message

Function call_user_method_array() is deprecated

Is there where I should use reflection? What exactly is it, and how would I use it to modify my code above to work as it used to?

+6  A: 

http://php.net/manual/en/function.call-user-method-array.php

The call_user_method_array() function is deprecated as of PHP 4.1.0.

New way:

<?php
// Old:
// call_user_method_array('view', $this, $args);
// New:
call_user_func_array(array($this, 'view'), $args);
?>
TiuTalk
Perfect! Thanks TiuTalk!
alex
A: 

From http://www.php.net/manual/en/function.call-user-method-array.php#89837

<?php

/**
 * @param string $func - method name
 * @param object $obj - object to call method on
 * @param boolean|array $params - array of parameters
 */
function call_object_method_array($func, $obj, $params=false){
    if (!method_exists($obj,$func)){        
        // object doesn't have function, return null
        return (null);
    }
    // no params so just return function
    if (!$params){
        return ($obj->$func());
    }        
    // build eval string to execute function with parameters        
    $pstr='';
    $p=0;
    foreach ($params as $param){
        $pstr.=$p>0 ? ', ' : '';
        $pstr.='$params['.$p.']';
        $p++;
    }
    $evalstr='$retval=$obj->'.$func.'('.$pstr.');';
    $evalok=eval($evalstr);
    // if eval worked ok, return value returned by function
    if ($evalok){
        return ($retval);
    } else {
        return (null);
    }        
    return (null);   
}

?>
Anax
-1 (and would downvote more if I could) for reinventing the wheel and using eval().
Luiz Damim
You could create a dummy SO account and double downvote me. I'll make sure mr brudinie at googlemail dot com, who proposed this solution (found at php.net doc site) and who's solution I clearly quoted here, is well aware of your opinion.
Anax