Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/264250-multiple-constructors/
Share on other sites

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.

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?

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.

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.