---
title: "发一个正在使用的购物车类的设计"
author: "binderyi"
date: "2011-05-20T11:09:10Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-05-20T11:09:10Z"
id: 1051388
url: "https://binderyi.iteye.com/blog/1051388"
---

购物车类是商城程序一个非常重要的类。写类，就要继承其可以重复利用的思想，高效，扩展性好的特点。
首先，我们要明白要实现怎么样的功能。然后根据功能的需求用代码实现。商城程序的购物车的基本功能有：
        1、添加商品
        2、移除商品
        3、计算总额
不是吧!这么简单？呵呵，购物车流程的确如此。现在就用代码来实现这个过程。
一个商品的固定基本资料有：
        1、商品唯一ID号(id)
        2、商品名称(name)
        3、零售价(retail_price)
        4、销售价(price)
变动的资料有：
        1、商品数量(quantity)
        2、折扣额(discount)
        3、小计(subtotal)
全部商品统计的资料有：
        1、总数量
        2、总金额
        3、总折扣(告诉顾客共节省**元)
由于html是无状态语言。对于保存购物车的状态数据可以用SESSION、COOKIE、数据库、文本文件。通常只选一种方式保存，但是每一种保存方式都各有优点。为了使程序更通用、更实用，把存储状态的操作不写入购物车类中，就同时兼容上面几种方法!用数组来实现。
        $abc
        $abc
        $abc
        $abc
        $abc
        $abc
如：
        Array
        (
                 => Array
                        (
                                 => 商品名称
                                 => 60
                                 => 50
                                 => 4
                                 => 40
                                 => 200
                        )
        }
下面以session方式实例来说明一下，所需用到类的设计的结果。
session_start();//初始化session
$cat = new x_cart(&$_SESSION);//以引用方式保存购物车状态的变量，也可以用COOKIE或普通变量存数据库或文本文件。
$cat->add_item(array(123,'风扇',60,50),2) ;//添加一个商品与数量。第一个参数是数组，数组依次是id、商品名、零售价、销售价(可选，如果没有给出销售价则销售价就是零售价)，第二个参数是商品数量
$cat->add_item(126,1);//同样的一个添加商品函数。有多种输入方式，增加程序操作的灵活性。第一个参数是商品id(可根据此ID自动调用商品名、零售价、销售价)，第二个参数是数量
$cat->remove_item(123,1) ;//减商品数量。第一个参数是商品id，第二个是减数量。如果id资料不存在。则返回失败。如果减数量刚好是购物车中数量，则删除此商品。
$cat->dele_item(124);//移除商品ID的全部数量
$cat->edit_item(126,2);//编辑购物车中商品的数量
print_r($cat->sum_item());//返回购物车的总数量、总折扣、总金额
echo '<pre>';
print_r($_SESSION);
$cat->empty_cart();//清空购物车
由此可见，每个方法在类中是如何工作的。根据需要写相对应的代码。下面就带注释详细代码

//<meta charset=UTF-8">
/*
　* 类    名: cart
　* 功    能: 购物车类
　* 版    本: 1
　* 日    期: 2006年09月07日
  * 修改次数: 0
  * 最后修改: 2006年09月09日
　* 作    者: qh663
　* 版    权: 清晖
  * oicq号码: 80031807
  * 邮    箱: binderyi@163.com
  * 主    页: http://www.gaoxiaotu5.com
　*/
class cart
{
        var $items;
        var $total;
        
        /**
         *构造函数
         */
        function cart($varname){
                $this->items = &$varname;//引用，绑定变量变化
        }
        /**
         *添加商品基本资料(类内部操作，私有)
         *如果$item是数组型，并且有3或4个数量，则赋值。如果是数字型，则预留get_merchandise函数方法取得内容。
         *否则就是程序设计的严重失败。(如非设计错误，而是由用户私改变量，可以考虑禁用此操作的用户)
         */
        function add_merchandise($item) {
                if (is_array($item) and (count($item) == 4 ) or count($item) == 3){
                        $id = (int)current($item);
                        if($this->is_item($id) == false){
                                $this->items = (string)next($item);
                                $this->items = next($item);
                                $price = next($item);
                                $this->items = empty($price)?$this->itemsprice;
                        }
                        return $id;
                } elseif (is_numeric($item)){
                        if($this->is_item($item)){
                                return $item;
                        } else {
                                if ($item = $this->get_merchandise($item)){
                                        return $this->add_merchandise($item);
                                } else {
                                        die('无此商品');//如非设计错误，用户可能私改变量，可以考虑禁用此操作的用户
                                }
                        }
                } else {
                        die('商品参数不正确!');//如非设计错误，用户可能私改变量，可以考虑禁用此操作的用户
                }
        }
        /**
         *添加一个商品
         *$item 数字或数组(依次是id、商品名、零售价、销售价<可选>)
         *$num 数量
         */
        function add_item($item, $num) {
                $id = $this->add_merchandise($item);
                $this->items += $num;
                $this->items = ($this->items - $this->items) * $this->items;//更新折扣
                $this->items = $this->items * $this->items;//更新小计
                return true;
        }
        /**
         *减商品
         */
        function remove_item($id, $num) {
                if ($this->is_item($id) == false) return false;//检测是否在购物车中的物品
                if ($this->items > $num) {//如果购物车中的物品大于要减的数量
                        $this->items -= $num;//从购物车中减出
                        $this->items = ($this->items - $this->items) * $this->items;//更新折扣
                        $this->items = $this->items * $this->items;//更新小计
                        return true;
                } elseif ($this->items == $num) {//如果购物车中的物品等于要减的数量
                        unset($this->items);//删除购物车中该物品的所有资料
                        return true;
                } else {
                        return false;
                }
        }
        /**
         *编辑商品数量
         */
        function edit_item($id, $num) {
                if ($this->is_item($id) == false) return false;//检测是否在购物车中的物品
                if ($num > 0) {//保存0件以上的商品放在购物车里
                        $this->items = $num;
                        $this->items = ($this->items - $this->items) * $this->items;//更新折扣
                        $this->items = $this->items * $this->items;//更新小计
                        return true;
                } elseif ($num == 0) {//如果是0件
                        unset($this->items);//删除购物车中该物品的所有资料
                        return true;
                } else {
                        return false;
                }
        }
        
        /**
         *删除购物车中该物品的所有资料
         */
        function dele_item($id) {
                if ($this->is_item($id) == false) return false;
                unset($this->items);
                return true;
        }

        /**
         *检测购物车中该物品是否存在
         */
        function is_item($id) {
                return is_array($this->items);
        }
        
        /**
         *清空购物车
         */
        function empty_cart(){
        $this->items = NULL;
    }
        
        /**
         *预留方法接口，根据ID取得商品信息源
         */
        function get_merchandise($id){
                return false;
        }
        /**
         *统计购物车
         */
        function sum_item(){
                if (!is_array($this->items)) return false;
                foreach ($this->items as $value){
                        $total += $value;//总数量
                        $total += $value;//总折扣
                        $total += $value;//总金额
                }
                $this->total = $total;
                return $total;
        }
        
}


class x_cart extends cart {
        /**
         *利用预留方法
         */
        function get_merchandise($id){
                $abc = array($id,'扩展',5.6,3.5);//此为测试数据，请用数据库操作取得相对应数组。
                return $abc;
        }
}

由此可见，我的设计方法是：分析=>由类执行过程=>类代码实现=>完成