5

Can you return false from the contructor?

<?php


class ftp_stfp{

    //private vars of the class
    private $host;
    private $username;
    private $password;
    private $connection_type;
    private $connection = false;


    function __contruct( $host, $username, $password, $connection_type ){

        //setting the classes vars
        $this->host         = $host;
        $this->username     = $username;
        $this->password     = $password;
        $this->connection_type = $connection_type;

        //now set the connection into this classes connection
        $this->connection = $this->connect();

        //check the connection was set else return false
        if($this->connection === false){
            return false;   
        } 
    } ... etc etc the rest of the class

Call the class:

$ftp_sftp = new ftp_sftp( $host, $uname, $pword, $connection_type );

Is this actually correct, ie would the $ftp_sftp var either be false or hold the class depending on outcome of the __construct method, or is this completely wrong logic?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262

4 Answers4

8

No. Constructors do not have return values. If you require getting some kind of result from a constructor you can do a few things:

If you need a return value either use a method to do the heavy lifting (usually called init()).

public static function init( $host, $username, $password, $connection_type ){

    //setting the classes vars
    $this->host         = $host;
    $this->username     = $username;
    $this->password     = $password;
    $this->connection_type = $connection_type;

    //now set the connection into this classes connection
    $this->connection = $this->connect();

    //check the connection was set else return false
    if($this->connection === false){
        return false;   
    } 
}

$ftp_sftp = ftp_sftp::init();

Store the results in a member variable and check its value after calling the constructor.

function __construct( $host, $username, $password, $connection_type ){

    //setting the classes vars
    $this->host         = $host;
    $this->username     = $username;
    $this->password     = $password;
    $this->connection_type = $connection_type;

    //now set the connection into this classes connection
    $this->connection = $this->connect();
}

$ftp_sftp = new ftp_sftp( $host, $uname, $pword, $connection_type );
if ($ftp_sftp->connection !== false)
{
    // do something
}

You can have your connect() method throw an exception. That will stop execution immediately and go to your catch block:

private method contect()
{
    // connection failed
    throw new Exception('connection failed!');
}

try 
{
    $ftp_sftp = new ftp_sftp( $host, $uname, $pword, $connection_type );
}
catch (Exception $e)
{
    // do something
}
zavg
  • 10,351
  • 4
  • 44
  • 67
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • yea i thought that would be the case, thank you for the rapid answer :) –  Jan 12 '13 at 17:16
4

Constructors can't return values. You can throw an exception in this stuation:

if($this->connection === false){
  throw new Exception('Connection can not be established.');  
}

Then you can instantiate variable in a try-catch block.

try
{
  $ftp_sftp = new ftp_sftp( $host, $uname, $pword, $connection_type );
}
catch(Exception $e)
{
  //Do whatever you want.
}
Gökhan Çoban
  • 600
  • 8
  • 17
0

There is an interesting thread on why the constructors have no return values Why do constructors not return values?.

Further it seems that by returning "False" you want to negate the object instantiation. If this is the case I would suggest that you thrown an exception if the connection fails and this way the object creation will fail.

Community
  • 1
  • 1
Ifthikhan
  • 1,484
  • 10
  • 14
0

I think you can create an object outside and directly use your connect() method and test there

$ftp_sftp = new ftp_sftp( $host, $uname, $pword, $connection_type );
$ftp_sftp->connect();

if connect is public.

A dev
  • 930
  • 3
  • 12
  • 27