search
Homephp教程PHP源码商城购物车以PHP单例模式实现。

1. [代码][PHP]代码   

<?php
class Cart{
    static protected $ins;          //实例变量
    protected $item = array();       //放商品容器
    //禁止外部调用
    final protected function __construct(){
    }

    //禁止克隆
    final protected function __clone(){
    }

    //类内部实例化
    static protected function Getins(){
        if(!(self::$ins instanceof self)){
            self::$ins = new self();
        }
        return self::$ins;
    }

    //为了能使商品跨页面保存,把对象放入session里
    public function Getcat(){
        if(!($_SESSION[&#39;cat&#39;]) || !($_SESSION[&#39;cat&#39;] instanceof self)){
            $_SESSION[&#39;cat&#39;] = self::Getins();
        }
        return $_SESSION[&#39;cat&#39;];
    }

    //入列时的检验,是否在$item里存在.
    public function Initem($goods_id){
        if($this->Gettype() == 0){
            return false;
        }
        if(!(array_key_exists($goods_id,$this->item))){
            return false;
        }else{
            return $this->item[$goods_id][&#39;num&#39;];   //返回此商品个数
        }
    }

    //添加一个商品
    public function Additem($goods_id,$name,$num,$price){
        if($this->Initem($goods_id) != false){
            $this->item[$goods_id][&#39;num&#39;] += $num;
            return;
        }
        $this->item[$goods_id] = array();               //一个商品为一个数组
        $this->item[$goods_id][&#39;num&#39;] = $num;           //这一个商品的购买数量
        $this->item[$goods_id][&#39;name&#39;] = $name;         //商品名字
        $this->item[$goods_id][&#39;price&#39;] = $price;       //商品单价
    }

    //减少一个商品
    public function Reduceitem($goods_id,$num){
        if($this->Initem($goods_id) == false){
            return;
        }
        if($num > $this->Getunm($goods_id)){
            unset($this->item[$goods_id]);
        }else{
            $this->item[$goods_id][&#39;num&#39;] -=$num;
        }
    }

    //去掉一个商品
    public function Delitem($goods_id){
        if($this->Initem($goods_id)){
            unset($this->item[$goods_id]);
        }
    }

    //返回购买商品列表
    public function Itemlist(){
        return $this->item;
    }

    //一共有多少种商品
    public function Gettype(){
        return count($this->item);
    }
    
    //获得一种商品的总个数
    public function Getunm($goods_id){
        return $this->item[$goods_id][&#39;num&#39;];
    }

    // 查询购物车中有多少个商品
    public function Getnumber(){
        $num = 0;
        if($this->Gettype() == 0){
            return 0;
        }
        foreach($this->item as $k=>$v){
            $num += $v[&#39;num&#39;];
        }

        return $num;
    }

    //计算总价格
    public function Getprice(){
        $price = 0;
        if($this->Gettype() == 0){
            return 0;
        }
        foreach($this->item as $k=>$v){
            $price += $v[&#39;num&#39;]*$v[&#39;num&#39;];
        }
        return $price;
    }

    //清空购物车
    public function Emptyitem(){
        $this->item = array();
    }
}
/*
     自己测试代码也拿出来
*/
?php
include_once(&#39;Cart.php&#39;);
$cart = Cart::Getcat();
$cart->Additem(&#39;1&#39;,&#39;谍匪&#39;,&#39;5&#39;,&#39;9999&#39;);
print_r($cart);

                   

                   

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool