PHP Developers Network
http://forums.devnetwork.net/

Best way to pass on arguments from __call
http://forums.devnetwork.net/viewtopic.php?f=50&t=120456
Page 1 of 1

Author:  TipPro [ Fri Aug 27, 2010 2:12 pm ]
Post subject:  Best way to pass on arguments from __call

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)...

Syntax: [ Download ] [ Hide ]
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();
            }
        }
    }
}
 

Author:  AbraCadaver [ Fri Aug 27, 2010 3:46 pm ]
Post subject:  Re: Best way to pass on arguments from __call

This should work (not tested):
Syntax: [ Download ] [ Hide ]
class FTPC {
    function __call($methodname, $args) {
        $functionName = "ftp_" . $methodname;

        if (function_exists($functionName)) {
           return call_user_func_array($functionName, $args);
        }
        return false;
    }
}

Author:  TipPro [ Fri Aug 27, 2010 7:32 pm ]
Post subject:  Re: Best way to pass on arguments from __call

Perfect - thank you very much!

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/