php 物件導向(不知道取什麼名字)

來源:互聯網
上載者:User
<?php /** * @version $Id$ * @create 2013-1-27 21:47:50 By xjiujiu * @description HongJuZi Framework * @copyRight Copyright (c) 2011-2012 http://www.xjiujiu.com.All right reserved */@header('Content-Type: text/html, charset=utf-8;');    //開啟session    @session_start();    include_once('./hongjuzi/hhcart.php');    //php 按行執行這雷根據傳來的action參數去調用其中的方法。    //其實柑橘在java中也是一樣的.    //在servlet中不也是通過指定方法才去調用doget和dopost方法麼。    //只不過這個是在method中指定的。    //如果要調用其他方法,也要自己去執行個體化的。    //而在struts2中,應該是用!去分割的。    $ac     = $_GET['ac'];    $cart   = new HHCart();switch($ac) {        case 'list':try{$list = $cart->getList();foreach($list as $key => $value){$num = $list[$key]['total'];$obj = $list[$key]['info'];                                        //stripslashes(String)去除斜杠$price = stripslashes($obj['price']);$count += intval($num * intval($price));}//echo json_encode(array('list' => $cart->getList()));              //建立數組,並複製給$orderList引用.期中list應該是一個對象數組。$orderList = array('list' => $cart->getList(),'count'=>$count);//array_push($orderList);                                //轉為json格式echo json_encode($orderList);}catch(Exception $ex){echo "失敗";}break;        case 'add': {            try {                //接受資料,並存為個一個數組。                $productInfo    = array(                    'id' => $_POST['productId'],                    'name' => $_POST['productName'],                    'price' => $_POST['price'],                    'type' => $_POST['productType']                );                //列印調試var_dump($productInfo);    $cart->add($productInfo);                echo json_encode(array('succeed' => true));            } catch(Exception $ex) {                echo json_encode(array(                    'succeed' => 'false',                    'info' => '貨物添加失敗!'                ));            }            break;        }        case 'sub': break;        case 'destroy': break;        default: break;    }?>

HHcart類

<?php /** * @version $Id$ * @create 2013-1-27 21:47:50 By xjiujiu * @description HongJuZi Framework * @copyRight Copyright (c) 2011-2012 http://www.xjiujiu.com.All right reserved *//** * 購物車類  *  * @desc *  * @author xjiujiu <xjiujiu@foxmail.com> * @package None * @since 1.0.0 */class HHCart{        /**     * @var Map $_cart 購物車容器     */    protected $_cart;    /**     * @var String $_cartId 當前的購物車ID     */    protected $_cartId;    /**     * @var String $_priceParam 單價名稱     */    protected $_priceParam;    /**     * @var int $_productIdParamName 當前的產品ID變量名     */    protected $_productIdParamName;    /**     * @var Mix $_productId 當前的產品ID     */    protected $_productId;    /**     * 構造函數      *      * @desc     *      * @author xjiujiu <xjiujiu@foxmail.com>     * @access public     * @param String $price 當前的價格變量名稱,默認:price     * @param String $productIdParamName 產品編號變量     */    public function __construct($price = 'price', $productIdParamName = 'id')    {        $this->_getCurCart();        $this->_cartId          = session_id();        $this->_productInfo     = null;        $this->_priceParam      = $price;        $this->_productIdParamName  = $productIdParamName;    }    /**     * 得到當前的購物車容器     *      * @desc     *      * @author xjiujiu <xjiujiu@foxmail.com>     * @access protected     */    protected function _getCurCart()    {        if(!isset($_SESSION['HHCART']) || !is_array($_SESSION['HHCART'])) {            $_SESSION['HHCART']     = array();        }        $this->_cart    = &$_SESSION['HHCART'];    }    /**     * 添加貨物      *      * @desc     *      * @author xjiujiu <xjiujiu@foxmail.com>     * @access public     */    public function add($productInfo)    {$productId   = $productInfo[$this->_productIdParamName];        if(!isset($this->_cart[$this->_cartId])) {            $this->_cart[$this->_cartId]    = array();        }    if(isset($this->_cart[$this->_cartId][$productId])) {            $this->_cart[$this->_cartId][$productId]['total'] ++;        } else {            $this->_cart[$this->_cartId][$productId]['total']    = 1;            $this->_cart[$this->_cartId][$productId]['info']     = $productInfo;        }}    /**     * 減少貨物      *      * @desc     *      * @author xjiujiu <xjiujiu@foxmail.com>     * @access public     * @return void     * @throws none     */    public function sub($productId)    {        if(isset($this->_cart[$this->_cartId])) {            if(isset($this->_cart[$this->_cartId][$productId])) {                if(1 != $this->_cart[$this->_cartId][$productId]['total']) {                    $this->_cart[$this->_cartId][$productId]['total'] -= 1;                } else {                    unset($this->_cart[$this->_cartId][$productId]['total']);                }            }        }    }    /**     * 清除貨物      *      * @desc     *      * @author xjiujiu <xjiujiu@foxmail.com>     * @access public     * @return void     * @throws none     */    public function delete($productId)    {        if(isset($this->_cart[$this->_cartId])) {            if(isset($this->_cart[$this->_cartId][$productId])) {                unset($this->_cart[$this->_cartId][$productId]);            }        }    }    /**     * 更新購物車      *      * @desc     *      * @author xjiujiu <xjiujiu@foxmail.com>     * @access public     * @return List 得到當前購物車裡的內容     */    public function getList()    {        return $this->_cart[$this->_cartId];    }    /**     * 清空      *      * @desc     *      * @author xjiujiu <xjiujiu@foxmail.com>     * @access public     * @return void     * @throws none     */    public function destroy()    {        unset($this->_cart[$this->_cartId]);    }    /**     * 結賬      *      * @desc     *      * @author xjiujiu <xjiujiu@foxmail.com>     * @access public     */    public function count()    {        $total  = 0;        foreach($this->_cart[$this->_cartId] as $product) {            $total  += intval($product['total']) * intval($product['info'][$this->_priceParam]);        }        return $total;    }}?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.