zoukankan      html  css  js  c++  java
  • 购物车单例模式

      1 <?php
      2 /**
      3  * php 购物车类
      4  * 单例模式
      5 */
      6  class Cart{
      7  static protected $ins; //实例变量
      8  protected $item = array(); //放商品容器
      9 
     10  //禁止外部调用
     11  final protected function __construct(){
     12  }
     13 
     14  //禁止克隆
     15  final protected function __clone(){
     16  }
     17 
     18  //类内部实例化
     19  static protected function Getins(){
     20  if(!(self::$ins instanceof self)){
     21  self::$ins = new self();
     22  }
     23 
     24  return self::$ins;
     25  }
     26 
     27  //为了能使商品跨页面保存,把对象放入session里
     28  public function Getcat(){
     29  if(!($_SESSION['cat']) || !($_SESSION['cat'] instanceof self)){
     30  $_SESSION['cat'] = self::Getins();
     31  }
     32 
     33  return $_SESSION['cat'];
     34  }
     35 
     36  //入列时的检验,是否在$item里存在.
     37  public function Initem($goods_id){
     38  if($this->Gettype() == 0){
     39  return false;
     40  }
     41  if(!(array_key_exists($goods_id,$this->item))){
     42  return false;
     43  }else{
     44  return $this->item[$goods_id]['num']; //返回此商品个数
     45  }
     46  }
     47 
     48  //添加一个商品
     49  public function Additem($goods_id,$name,$num,$price){
     50  if($this->Initem($goods_id) != false){
     51  $this->item[$goods_id]['num'] += $num;
     52  return;
     53  }
     54 
     55  $this->item[$goods_id] = array(); //一个商品为一个数组
     56  $this->item[$goods_id]['num'] = $num; //这一个商品的购买数量
     57  $this->item[$goods_id]['name'] = $name; //商品名字
     58  $this->item[$goods_id]['price'] = $price; //商品单价
     59  }
     60 
     61  //减少一个商品
     62  public function Reduceitem($goods_id,$num){
     63  if($this->Initem($goods_id) == false){
     64  return;
     65  }
     66  if($num > $this->Getunm($goods_id)){
     67  unset($this->item[$goods_id]);
     68  }else{
     69  $this->item[$goods_id]['num'] -=$num;
     70  }
     71  }
     72 
     73  //去掉一个商品
     74  public function Delitem($goods_id){
     75  if($this->Initem($goods_id)){
     76  unset($this->item[$goods_id]);
     77  }
     78  }
     79 
     80  //返回购买商品列表
     81  public function Itemlist(){
     82  return $this->item;
     83  }
     84 
     85  //一共有多少种商品
     86  public function Gettype(){
     87  return count($this->item);
     88  }
     89 
     90  //获得一种商品的总个数
     91  public function Getunm($goods_id){
     92  return $this->item[$goods_id]['num'];
     93  }
     94 
     95  // 查询购物车中有多少个商品
     96  public function Getnumber(){
     97  $num = 0;
     98  if($this->Gettype() == 0){
     99  return 0;
    100  }
    101 
    102  foreach($this->item as $k=>$v){
    103  $num += $v['num'];
    104  }
    105  return $num;
    106  }
    107 
    108  //计算总价格
    109  public function Getprice(){
    110  $price = 0;
    111  if($this->Gettype() == 0){
    112  return 0;
    113  }
    114 
    115  foreach($this->item as $k=>$v){
    116  $price += $v['num']*$v['num'];
    117  }
    118  return $price;
    119  }
    120 
    121  //清空购物车
    122  public function Emptyitem(){
    123  $this->item = array();
    124  }
    125  }
    126 ?>
    <?php
    session_start();
    header("Content-type = text/html; charset=utf-8");
    include_once('Cart.php');
    $cart = Cart::Getcat();
    $cart->Additem('1','php学习教程(脚本学堂版)','5','9999');
    
    $cart->Additem('1','php学习教程(脚本学堂版)','52','9999');
    $cart->Additem('2','php学习教程(脚本学堂版)2','52','99992');
    print_r($cart);
    var_dump($_SESSION['cat']);
    ?>
  • 相关阅读:
    十.总结drf视图
    一.9.多云管理同步服务器
    一.vue 初识
    一.8.django权限管理/drf权限管理
    一.7.服务器之分页和搜索应用
    一.6.序列化应用之服务器同步功能
    一.5.序列化应用之服务器制造厂与型号app功能
    【前端背景UI】鼠标磁性动态蜘蛛网背景源码
    【vue】导入式,使用vue与element UI, 单人开发项目,告别脚手架
    【python后台admin】所有属性显示设置
  • 原文地址:https://www.cnblogs.com/jami918/p/3728291.html
Copyright © 2011-2022 走看看