yoursurrogategod Posted June 15, 2012 Share Posted June 15, 2012 Hi guys, I have a question for you. This is what I'm trying to do: <?php require_once("FTP.php"); /** * Fax.php: * This is a utility to fax documents using FTP. */ class Lifespan_Fax_Fax extends Zend_View_Helper_Abstract { /** * This is where we will keep the FTP connection object alive. * @var object FTP */ private $ftpConnection; public function __construct() { $this->ftpConnection = new FTP("", "", "", ""); } public function __construct($server = '', $port = '', $username = '', $password = '') { $this->ftpConnection = new FTP($server, $port, $username, $password); } public function send($directory, $filename, $data) { $this->ftpConnection->writeToFTPServer } } In C++ and Java, this is 100% ok. However, how can I do this in PHP? I was thinking of a bunch of if-statements to check whether a variable is '' or not, but that seems messy. Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/264250-multiple-constructors/ Share on other sites More sharing options...
requinix Posted June 15, 2012 Share Posted June 15, 2012 Just like with every. other. place. in PHP, you can't have more than one function with the same name. Besides, those two constructors have the exact same functionality. Just remove the first one and you won't even know it's gone. Quote Link to comment https://forums.phpfreaks.com/topic/264250-multiple-constructors/#findComment-1354195 Share on other sites More sharing options...
yoursurrogategod Posted June 15, 2012 Author Share Posted June 15, 2012 Just like with every. other. place. in PHP, you can't have more than one function with the same name. Besides, those two constructors have the exact same functionality. Just remove the first one and you won't even know it's gone. I'm not very experienced in PHP, so I don't know. As it stands, yes, but when the = '' are removed, then no. Furthermore, how would you do what I described? Quote Link to comment https://forums.phpfreaks.com/topic/264250-multiple-constructors/#findComment-1354196 Share on other sites More sharing options...
KevinM1 Posted June 15, 2012 Share Posted June 15, 2012 In PHP, when you have an argument that is equal to something, like: example($x = "hello") { // blah } That value is that argument's default value. So, if you invoke the function like so: example(); It will use that default value (the string "hello" in this case). If you supply a different value, like: example("bonjour"); It will use that instead. So, in your case, you don't need two constructors as your second constructor can take parameters, but if no parameters are supplied, it will just use the specified default values, which are empty strings in your case. Quote Link to comment https://forums.phpfreaks.com/topic/264250-multiple-constructors/#findComment-1354200 Share on other sites More sharing options...
yoursurrogategod Posted June 15, 2012 Author Share Posted June 15, 2012 In PHP, when you have an argument that is equal to something, like: example($x = "hello") { // blah } That value is that argument's default value. So, if you invoke the function like so: example(); It will use that default value (the string "hello" in this case). If you supply a different value, like: example("bonjour"); It will use that instead. So, in your case, you don't need two constructors as your second constructor can take parameters, but if no parameters are supplied, it will just use the specified default values, which are empty strings in your case. Ok, so, if I wanted to simulate default constructor functionality as one sees in C++/Java, I would do the following, yes? public function __construct($server = '', $port = '', $username = '', $password = '') { if (('' == $server) || ('' == $port) || ('' == $username) || ('' == $password)) { $server = ftpofdoom.org'; $port = '10021'; $username = 'user'; $password = 'pass'; } $this->ftpConnection = new FTP($server, $port, $username, $password); } Now, what if I wanted to not even have the if-statement and then proceed to just set those input values? Quote Link to comment https://forums.phpfreaks.com/topic/264250-multiple-constructors/#findComment-1354202 Share on other sites More sharing options...
requinix Posted June 15, 2012 Share Posted June 15, 2012 Default values for each? public function __construct($server = 'ftpofdoom.org', $port = 10021, $username = 'user', $password = 'pass') { If you need some kind of logic (like getting the default values from someplace) then yes, you would need something like that code. public function __construct($server = null, $port = null, $username = null, $password = null) { if (empty($server)) $server = // default value if (empty($port)) $port = // default value if (empty($username)) $username = // default value if (empty($password)) $password = // default value } You should actually do something even more complicated than that. Like if I give a $server but no $port then really the port should be the default 21, meanwhile no server and no port means pull both values from somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/264250-multiple-constructors/#findComment-1354207 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.