2012-04-23 7 views
0

このコードは同じものを出力します。私の質問は、これを行う正しい方法は何かです。最初のアプローチか2番目のアプローチですか?それとも良い方法がありますか?私はあるクラスの利点を他のものよりも見ない。クラスの変数 - 正しいアプローチ

<?php 
    class Client{ 
     var $id; 
     var $email; 

     function __construct($id){ 
      $this->id=$id; 
     } 

     public function get_email($db){ 
      $sql = $db -> prepare(" SELECT email FROM users WHERE id = ? "); 

      $sql -> bind_param('i', $this->id); 
      $sql->execute(); 

      $sql->bind_result($email); 

      if ($sql -> fetch()) { 
       return $this->email=$email; 
      } 
      else 
      return false; 
     } 
    } 

    class Client_{ 
     public function get_email($db, $id){ 
      $sql = $db -> prepare(" SELECT email FROM users WHERE id = ?"); 

      $sql -> bind_param('i', $id); 
      $sql->execute(); 

      $sql->bind_result($email); 

      if ($sql -> fetch()) { 
       return $email; 
      } 
      else 
      return false; 
     } 
    } 
    ?> 

のindex.php

<?php 
$Client = new Client(1); 
$a = $Client -> get_email($db); 

print_r($a); 

$Client_ = new Client_(); 
$b = $Client_ -> get_email($db, 1); 

print_r($b); 
?> 
+0

なぜ '$ id'を文字列として渡していますか?それはちょうどint、ちょうどことができますか? – Jon

+0

私はコンストラクタで '$ db'を渡した後、何らかのデータが必要になるたびに関数' get_email'を実行したほうがいいと思います。 – hamczu

+0

"より正確な"ことは、クラスで表現したりモデル化したいことによって異なります。 – deceze

答えて

1

将来の使用のために記憶されるべきものはないとして、それは、クラスをインスタンス化しても意味がありません。

class Client_{ 
    static public function get_email($db, $id){ 
     $sql = $db -> prepare(" SELECT email FROM users WHERE id = ?"); 

     $sql -> bind_param('i', $id); 
     $sql->execute(); 

     $sql->bind_result($email); 

     if ($sql -> fetch()) { 
      return $email; 
     } 
     else 
     return false; 
    } 
} 

// And use it static way without instantiating first: 
Client_::get_email($arg1, $arg2); 

私は最初のものを取るつもりですこれら二つの間で決定を下すshoul場合:だから、様々なデータがどこか別の場所に保存されている場合は、「静的クラス」を使用する方がよいでしょう。

私はあなたがこれらのクラスのいずれかを使用しようとしている方法を知っているが、それでも私のためにそれが$dbを保存し、外部から$idを供給し、$emailをローカルにするために、より理にかなっていけない:

class Client{ 
    var $db; 

    function __construct($db){ 
     $this->db=$db; 
    } 

    public function get_email($id){ 
     $sql = $this->db -> prepare(" SELECT email FROM users WHERE id = ? "); 

     $sql -> bind_param('i', $id); 
     $sql->execute(); 

     $sql->bind_result($email); 

     if ($sql -> fetch()) { 
      return $email; 
     } 
     else 
     return false; 
    } 
} 

もこれを変更します行:return $this->email=$email;、多分私は間違っているが、私はそれがちょうど意味がないと思う。

0

私は、クラス名はクラスがデータベーステーブルのモデルであることを示しているので、最初のものは、このシナリオではより多くの「正しい」だと思い、つまり、テーブルの変更のほとんどまたはすべてを、そのクラスを通して抽象化する必要があります。多くの場合、モデルクラスのインスタンス化はデータベーステーブルの1つの行を表します。したがって、idはクラスのメンバーです。

また、データベース接続をコンストラクタを介してモデルに渡すか、何らかの形でそれをグローバルに使用できるようにします。

^第二のアプローチはちょうど私の2セント

0

varを使用して、PHPでクラス変数を定義しないでください。もう4.xではありません。今どこにpublicprivateprotectedがあります。

class Client 
{ 
    protected $connection; 
    protected $id; 
    protected $email = null; 

    public function __construct($connection, $id) 
    { 
     $this->connection = $connection; 
     $this->id = $id 
    } 

    public function getEmail() 
    { 
     if ($this->email === null) 
     { 
      $query = 'SELECT email FROM users WHERE id = ?'; 
      $statement = $this->connection->prepare($query); 
      $statement->bind_param('i', $this->id); 
      if ($statement->execute()) 
      { 
       $statement->bind_result($this->email); 
      } 
     } 
     return $this->email; 
    } 

} 

P.S.:Clientのインスタンスは両方の接続とidentificatorを必要とする、前記

私は実際に接続APIのMySQLiの代わりにPDOを好むでしょう。他の理由がない場合は、エラー処理の柔軟性が高いからです。

関連する問題