Best way to pass on arguments from __call

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
TipPro
Forum Commoner
Posts: 35
Joined: Wed Mar 15, 2006 6:39 pm

Best way to pass on arguments from __call

Post by TipPro »

I am fairly new to object oriented programming and I am playing around with creating a wrapper class that takes advantage of the overloading method __call.

I am having trouble passing on the arguments. If all the arguments were always strings it would be easy as implode(', ', $arguments)

Here is a something I came up with for a (very rough) FTP wrapper class but it is redundant and I am wondering if someone has a better way of doing this (plus this would not work for functions over five arguments)...

Code: Select all

class FTPC {
    function __call($methodname, $args) {
        $functionName = "ftp_" . $methodname;
        if (function_exists($functionName)) {
            switch (count($args)) {
                case 1: return $functionName($args[0]);
                case 2: return $functionName($args[0], $args[1]);
                case 3: return $functionName($args[0], $args[1], $args[2]);
                case 4: return $functionName($args[0], $args[1], $args[2], $args[3]);
                case 5: return $functionName($args[0], $args[1], $args[2], $args[3], $args[4]);
                default: return $functionName();
            }
        }
    }
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best way to pass on arguments from __call

Post by AbraCadaver »

This should work (not tested):

Code: Select all

class FTPC {
    function __call($methodname, $args) {
        $functionName = "ftp_" . $methodname;

        if (function_exists($functionName)) {
           return call_user_func_array($functionName, $args);
        }
        return false;
    }
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
TipPro
Forum Commoner
Posts: 35
Joined: Wed Mar 15, 2006 6:39 pm

Re: Best way to pass on arguments from __call

Post by TipPro »

Perfect - thank you very much!
Post Reply