4

建設業者からfalseを返すことはできますか?

<?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

クラスを呼び出す:

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

これは実際に正しいですか、つまり、$ ftp_sftp varはfalseであるか、__constructメソッドの結果に応じてクラスを保持しますか、それともこれは完全に間違ったロジックですか?

4

4 に答える 4

8

いいえ。コンストラクターには戻り値がありません。コンストラクターから何らかの結果を取得する必要がある場合は、いくつかのことを実行できます。

戻り値が必要な場合は、メソッドを使用して手間のかかる作業を行います(通常はと呼ばれ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();

結果をメンバー変数に格納し、コンストラクターを呼び出した後にその値を確認します。

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
}

connect()メソッドに例外をスローさせることができます。catchそれはすぐに実行を停止し、あなたのブロックに行きます:

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
}
于 2013-01-12T17:15:45.450 に答える
4

コンストラクターは値を返すことができません。この状態で例外をスローできます。

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

次に、try-catchブロックで変数をインスタンス化できます。

try
{
  $ftp_sftp = new ftp_sftp( $host, $uname, $pword, $connection_type );
}
catch(Exception $e)
{
  //Do whatever you want.
}
于 2013-01-12T17:21:12.790 に答える
1

コンストラクターに戻り値がない理由について興味深いスレッドがあります。コンストラクターが値を返さないのはなぜですか。

さらに、「False」を返すことにより、オブジェクトのインスタンス化を無効にしたいようです。この場合、接続が失敗すると例外がスローされ、この方法でオブジェクトの作成が失敗することをお勧めします。

于 2013-01-12T17:21:56.333 に答える
1

外部でオブジェクトを作成し、connect()メソッドを直接使用してそこでテストできると思います

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

接続がパブリックの場合。

于 2013-01-12T17:40:12.930 に答える