PHP Doku:: Call a user method given with an array of parameters [deprecated] - function.call-user-method-array.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenKlassen- und ObjektinformationenKlassen- und Objekt-Funktionencall_user_method_array

Ein Service von Reinhard Neidl - Webprogrammierung.

Klassen- und Objekt-Funktionen

<<Klassen- und Objekt-Funktionen

call_user_method>>

call_user_method_array

(PHP 4 >= 4.0.5, PHP 5)

call_user_method_arrayCall a user method given with an array of parameters [deprecated]

Beschreibung

mixed call_user_method_array ( string $method_name , object &$obj , array $params )
Warnung

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

Parameter-Liste

method_name

The method name being called.

obj

The object that method_name is being called on.

params

An array of parameters.

Beispiele

Beispiel #1 call_user_method_array() alternative

<?php
call_user_func_array
(array($obj$method_name), $params);
call_user_func_array(array(&$obj$method_name), $params); // PHP 4
?>

Siehe auch


3 BenutzerBeiträge:
- Beiträge aktualisieren...
wloche at hotmail dot com
15.07.2010 10:33
You don't have to write a new function, <?php call_user_func_array(array($obj, $method_name), $params); ?> works pretty fine! (to my mind, 'eval' fucntion should be avoided almost all the time)
brudinie at googlemail dot com
25.03.2009 16:23
<?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);  
}

?>
leonwinter at gmail dot com
2.10.2006 13:45
If you this function will be deleted in future php releases you can use this function:

<?
      
function _call_user_method_array($name, $obj, $params) {
          
// get params
          
$txt = "";
           foreach(
$params as $nr => $el) {
              
$txt.='$params['.$nr."], ";
           }
          
$txt = substr($txt, 0, strlen($txt)-2);
          
// run it
          
return eval('return call_user_method($name, $obj, '.$txt.');');          
       }
?>

Hope this helps somebody.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)