【发布时间】:2010-11-06 11:04:30
【问题描述】:
所以我有一个项目类如下:
类项目 { 私人 $db; 私人 $data = 数组( 'AltItem1' => 空, 'AltItem2' => 空, 'BaseUOM' => 空, 'Category1' => 空, 'Category2' => 空, 'Category3' => 空, 'Category4' => 空, 'Iden' => 空, 'IsHCS' => 空, 'ItemDesc' => 空, 'ItemNmbr' => 空, '项目类型' => 空, 'MSDS' => 空, 'NoteText' => 空, '非库存' => 空, 'PrcLevel' => 空, 'TipPrice' => 空, 'DTM_UpdType' => 空, 'DTM_UpdDateTime' => 空, 'DTM_DownloadDateTime' => 空, 'DTM_UploadDateTime' => 空 ); 公共函数 __construct(mysqli $db, $id = null){ $this->db = $db; if(!empty($id)){ $id = (int)$id; $this->populate($id); } } 公共函数 __get($key) { if(array_key_exists($key, $this->data)){ 返回 $this->data[$key]; } error_log("无效键 '$key'"); 返回空值; } 公共函数 __set($key, $value) { if(array_key_exists($key, $this->data)){ $this->data[$key] = $value; 返回真; } 返回假; } 公共函数填充($id) { $sql = sprintf( "从 ItemMaster WHERE id = 中选择 %s?", 内爆(“,”,array_keys($this->data)) ); $stmt = $this->db->stmt_init(); $stmt->prepare($sql) or die ("Could not prepare statement:" . $stmt->error); $stmt->bind_param('i', $id); $stmt->execute() 或 die('exec'); $stmt->store_result(); 如果($stmt->num_rows == 1) { $params = 数组(); foreach($this->data as $key => $val){ $params[] = &$this->data[$key]; } call_user_func_array(array($stmt, 'bind_result'), $params); $stmt->fetch(); $返回=真; } 别的{ user_error("没有为 id '$id' 返回行"); $返回=假; } 返回 $return; } 公共函数插入() { $params = $this->数据; $values = 数组(); foreach($params 作为 $param){ $values[] = "?"; } $sql = sprintf( "插入重复 (%s) 值 (%s)", 内爆(“,”,array_keys($params)), 内爆(“,”,$值) ); $stmt = $this->db->stmt_init(); $stmt->prepare($sql) or die ("Could not prepare statement:" . $stmt->error); $types = str_repeat("s", count($params)); array_unshift($params, $types); call_user_func_array(array($stmt, "bind_param"), $params); $stmt->执行(); $stmt->store_result(); $result = $stmt->result_metadata(); } 公共函数更新() { $sql = "更新重复设置"; $params = 数组(); foreach($this->data as $key => $value){ $params[] = "$key = ?"; } $sql .= implode(", ", $params) 。 "哪里 id = ?"; $stmt = $this->db->stmt_init(); $stmt->prepare($sql) or die ("Could not prepare statement:" . $stmt->error); $params = $this->数据; $params[] = $this->data['id']; $types = str_repeat("s", count($params)); array_unshift($params, $types); call_user_func_array(array($stmt, "bind_param"), $params); $stmt->执行(); $stmt->store_result(); $result = $stmt->result_metadata(); } }我的问题是,用我拥有的数据结构扩展此类的最佳方法是什么?我基本上想要购物车中的物品的另一个类。所以一些额外的字段会是数量、购物车 id 等。或者有没有更好的方法可以在不扩展类的情况下做到这一点?
另一方面,假设我有另一个变量 $price 不直接存储在数据库中。所以我将它设为公共变量,但我必须使用辅助方法来访问它,不是吗?如果是这样的话,我的 $data 数组是不是这类项目的最佳解决方案?
提前致谢。
【问题讨论】:
标签: php class oop object extension-methods