PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Dec 13, 2019 10:25 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Aug 27, 2010 2:12 pm 
Offline
Forum Commoner

Joined: Wed Mar 15, 2006 7:39 pm
Posts: 35
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();
            }
        }
    }
}
 


Top
 Profile  
 
PostPosted: Fri Aug 27, 2010 3:46 pm 
Offline
DevNet Master
User avatar

Joined: Mon Feb 24, 2003 11:12 am
Posts: 2572
Location: The Republic of Texas
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;
    }
}

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


Top
 Profile  
 
PostPosted: Fri Aug 27, 2010 7:32 pm 
Offline
Forum Commoner

Joined: Wed Mar 15, 2006 7:39 pm
Posts: 35
Perfect - thank you very much!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group