PHP单例模式实现商城购物车功能

PHP单例模式实现商城购物车功能-PHP实例教程完成这个小功能必须会写需求 养成好的习惯 Php购物车的开发需求功能如下 1: 购物车放在session里 2: 单例模式来开发 功能: 增删改查 增一个商品 减少

  PHP单例模式实现商城购物车功能-PHP实例教程完成这个小功能必须会写需求 养成好的习惯

  Php购物车的开发需求功能如下:php100.com

  1: 购物车放在session里

  2: 单例模式来开发

  功能:

  增删改查

  增一个商品

  减少一个商品(改,数量)

  删

  去掉一个商品

  清空购物车

  查:

  返回所有商品列表

  一共有几种商品

  一共有几个商品

  购物车里商品一共多少钱

  当你了解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['cat']) || !($_SESSION['cat'] instanceof self)){

  $_SESSION['cat'] = self::Getins();

  }

  return $_SESSION['cat'];

  }

  //入列时的检验,是否在$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]['num']; //返回此商品个数

  }

  }

  //添加一个商品

  public function Additem($goods_id,$name,$num,$price){

  if($this->Initem($goods_id) != false){

  $this->item[$goods_id]['num'] += $num;

  return;

  }

  $this->item[$goods_id] = array(); //一个商品为一个数组

  $this->item[$goods_id]['num'] = $num; //这一个商品的购买数量

  $this->item[$goods_id]['name'] = $name; //商品名字

  $this->item[$goods_id]['price'] = $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]['num'] -=$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]['num'];

  }

  // 查询购物车中有多少个商品

  public function Getnumber(){

  $num = 0;

  if($this->Gettype() == 0){

  return 0;

  }

  foreach($this->item as $k=>$v){

  $num += $v['num'];

  }

  return $num;

  }

  //计算总价格

  public function Getprice(){

  $price = 0;

  if($this->Gettype() == 0){

  return 0;

  }

  foreach($this->item as $k=>$v){

  $price += $v['num']*$v['num'];

  }

  return $price;

  }

  //清空购物车

  public function Emptyitem(){

  $this->item = array();

  }

  }

  /*

  自己测试代码也拿出来

  */

  ?php

  include_once('Cart.php');

  $cart = Cart::Getcat();

  $cart->Additem('1','谍匪','5','9999');

  print_r($cart);


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值