我爲客戶創建了一個基本類。訪問類中的數組集合的最佳實踐
我以前沒有這樣做,想知道訪問數據的最佳方式。
對於客戶數組中的每個字段,我是否應該有一個get()方法,或者我應該簡單地將客戶數組傳回並訪問該頁面。
即剛好返回數組
class Customer {
protected $id;
protected $customer;
public function __construct($customer_id) {
$this->id = $customer_id;
$this->set_customer();
}
protected function set_customer() {
$query = mysql_query("SELECT * FROM customer WHERE id = '$this->id'");
$this->customer = mysql_fetch_row($query);
}
public function get_customer() {
return $this->customer;
}
}
與陣列中創建的每個項目的方法
class Customer {
protected $id;
protected $customer;
public function __construct($customer_id) {
$this->id = $customer_id;
$this->set_customer();
}
protected function set_customer() {
$query = mysql_query("SELECT * FROM customer WHERE id = '$this->id'");
$this->customer = mysql_fetch_row($query);
}
public function get_customer_name() {
return $this->customer->customer_name;
}
...
...
}
與基於Tobias的反饋選項3:(如果不知道語法是正確的)
class Customer {
protected $id;
protected $customer;
public function __construct($customer_id) {
$this->id = $customer_id;
return $this->set_customer();
}
protected function set_customer() {
$query = mysql_query("SELECT * FROM customer WHERE id = '$this->id'");
return mysql_fetch_row($query);
}
}
托比亞斯,作爲一個新手,我可以通過改變以下幾行來達到你的建議嗎? 舊:$ this-> set_customer(); new:return $ this-> set_customer(); AND old:$ this-> customer = mysql_fetch_row($ query); new:return mysql_fetch_row($ query); – 2010-05-27 01:30:18
是的,那應該工作:) – Tobias 2010-05-27 18:10:56