我为客户创建了一个基本类。访问类中的数组集合的最佳实践
我以前没有这样做,想知道访问数据的最佳方式。
对于客户数组中的每个字段,我是否应该有一个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