PHP fonksiyon şimdi ne kullanmalıyım, kalktı?

2 Cevap php

Benim sınıflardan birinde bu kodu var

 public function __call($method, $args) {

        array_unshift($args, $method);

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

    }

Biz bu yana sunucuları açık ettik ve onlar PHP5 yeni bir sürümünü kullanmanız gerekir, ve ben şu mesajı alıyorum

Function call_user_method_array() is deprecated

Nerede kullanmanız gerektiğini mi var reflection? Tam olarak ne olduğunu ve onu eskisi gibi nasıl çalışması için yukarıda benim kodunu değiştirmek için kullanmak istiyorsunuz?

2 Cevap

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

Call_user_method_array () işlevi PHP 4.1.0 itibariyle önerilmemektedir.

New way:

<?php
// Old:
// call_user_method_array('view', $this, $args);
// New:
call_user_func_array(array($this, 'view'), $args);
?>

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

?>