2

私はウェブサイトから一人でPHPを学んでいます。他の言語に関するOOPのバックグラウンドがあります。
質問1:
これは、2つの異なるパラメーターセットを使用してPHPでコンストラクターを実装する正しい方法ですか?
質問2:
PHPで__setマジックメソッドを使用する場合、このクラスのcomment_idを__set関数から変更できないようにします。これはおそらくPHPで実行できますか?

 class Comment{
    private $comment_id;
    private $image_id;
    private $author_id;
    private $comment_text;
    private $created_at;

    public function __construct()
    {
        $arguments = func_get_args();
        $num = sizeof($arguments)
        switch($num)
        {
            case 0;
                break;
            case 3:
                this->image_id = $arguments[0];
                this->author_id = $arguments[1];
                this->comment_text = $argument[2];
                break;
            case 5:
                this->comment_id = $arguments[0];
                this->image_id = $arguments[1];
                this->author_id = $argument[2];
                this->comment_text = $argument[3];
                this->created_at = $argument[4];
                break;
            default:
                break;

        }
    }

    public function __get($property)
    {
        if(property_exists($this,$property))
        {
            return $this->$property;
        }
    }

    public function __set($property_name,$value)
    {
        if(property_exists($this,$property_name))
        {
            $this->$property_name = $value; 
        }

    }
    }
4

4 に答える 4

2
  1. PHPで「複数の」コンストラクターを宣言する方法はたくさんありますが、「正しい」方法ではありません(PHPでは技術的に許可されていないため)。しかし、あなたがそこでそれをどのように行っているかについては何も悪いことは見ていません。詳細については、このStackOverflowの質問をご覧ください。

  2. ifステートメントを使用するだけで済みます。このようなもの、おそらく?

    public function __set($property_name,$value)
    {
        $hidden_properties = array(
            'comment_id',
            'any_other_properties'
        );
    
        if(!in_array($property_name, $hidden_properties) &&
           property_exists($this, $property_name))
        {
            $this->$property_name = $value; 
        }
    }
    
于 2012-04-29T10:34:27.153 に答える
1

ここですでに示したように、PHPで複数のコンストラクターを実行するための最良の方法として、PHPでコンストラクターを宣言する方法はたくさんありますmultiple。別の例を次に示します。

<?php

class myClass {
    public function __construct() {
        $get_arguments       = func_get_args();
        $number_of_arguments = func_num_args();

        if (method_exists($this, $method_name = '__construct'.$number_of_arguments)) {
            call_user_func_array(array($this, $method_name), $get_arguments);
        }
    }

    public function __construct1($argument1) {
        echo 'constructor with 1 parameter ' . $argument1 . "\n";
    }

    public function __construct2($argument1, $argument2) {
        echo 'constructor with 2 parameter ' . $argument1 . ' ' . $argument2 . "\n";
    }

    public function __construct3($argument1, $argument2, $argument3) {
        echo 'constructor with 3 parameter ' . $argument1 . ' ' . $argument2 . ' ' . $argument3 . "\n";
    }
}

$object1 = new myClass('BUET');
$object2 = new myClass('BUET', 'is');
$object3 = new myClass('BUET', 'is', 'Best.');

出典:複数のコンストラクターを使用および理解するための最も簡単な方法:

お役に立てれば。

于 2015-01-24T06:55:49.933 に答える
0

質問1については、この投稿をチェックしてくださいPHPで複数のコンストラクターを実行するための最良の方法

質問2の場合、__set関数でcomment_id属性を除外できます。

public function __set($property_name,$value)
{
    if (property_exists($this, $property_name) && $property_name !== 'comment_id')
    {
        $this->$property_name = $value; 
    }

}
于 2012-04-29T10:29:05.280 に答える
0

質問1

これは、2つの異なるパラメーターセットを使用してPHPでコンストラクターを実装する正しい方法ですか?

はい(他のポスターが指摘しているように、少なくとも正しい方法です。私が考えることができる1つの改善は、defaultブランチで例外をスローすることです。

質問2

PHPで__setマジックメソッドを使用する場合、このクラスのcomment_idを__set関数から変更できないようにします。これはおそらくPHPで実行できますか?

確かに、あなたはほとんどそこにいます:

public function __set($property_name,$value) {
  if(property_exists($this,$property_name)) {
    if($property_name == "comment_id") {
      // throw an exception? ignore?
    } else {
      $this->$property_name = $value;
    }
  } else {
    throw new Exception(strtr("Class {class} does not have property {property}!", array(
      '{class}' => get_class($this),
      '{property}' => $property_name,
    ));
  }
}

ただし、ここには制限があります。このコメントを参照してください。

于 2012-04-29T10:31:17.967 に答える