note 98012 added to function.call-user-method-array

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Small, but necessary change:

Remember: eval returns null, unless return is called within eval'd string.
So check, that result is not false (bool).

Hint:
Using classes that gets functionality from external sources, i.e. SoapClient, which gets functions from wdsl, the method_exists -check in the beginning will fail.
Maybe you want to add a parameter to the function to supress the check that method exists.

<?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 !== false){
        return ($retval);
    } else {
        return (null);
    }       
    return (null);  
}

?>
----
Server IP: 212.124.37.9
Probable Submitter: 213.209.121.3
----
Manual Page -- http://www.php.net/manual/en/function.call-user-method-array.php
Edit        -- https://master.php.net/note/edit/98012
Del: integrated  -- https://master.php.net/note/delete/98012/integrated
Del: useless     -- https://master.php.net/note/delete/98012/useless
Del: bad code    -- https://master.php.net/note/delete/98012/bad+code
Del: spam        -- https://master.php.net/note/delete/98012/spam
Del: non-english -- https://master.php.net/note/delete/98012/non-english
Del: in docs     -- https://master.php.net/note/delete/98012/in+docs
Del: other reasons-- https://master.php.net/note/delete/98012
Reject      -- https://master.php.net/note/reject/98012
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.