Home >php教程 >php手册 >单例模式实现购物车

单例模式实现购物车

WBOY
WBOYOriginal
2016-06-07 11:42:101247browse

采用单例模式写的购物车
session_start();
/**
*单例模式购物车(SESSION购物车)
*/
class CartTool{
private static $ins = null;
private $items = array();
public $rand = 0;

protected function __construct(){
header("Content-type: text/html; charset=utf-8");
$this->rand = mt_rand(1,99999);
}

/**
* 防止被复制
*/
final protected function __clone(){ }

/**
* 获取实例
* */
protected static function getIns(){
//判断该实例是否被实例化
if(!(self::$ins instanceof self)){
self::$ins = new self();
}

return self::$ins;
}

/**
* 把购物车的单例对象放到session里
* */
public static function getCart(){
//判断购物车是否存在,并且该购物车是否是我的
if(!isset($_SESSION['cart']) || !($_SESSION['cart'] instanceof self)){
$_SESSION['cart'] = self::getIns();
}

return $_SESSION['cart'];
}

/**
* 添加商品
* @param $id 商品id
* @param $name 商品名称
* @param $price 商品价格
* @param $num 商品数量
*/
public function addItem($id,$name,$price,$num=1){
if($this->hasItem($id)){//如果商品存在,则数量相加
$this->incNum($id,$num);
return ;
}

$item = array();
$item['name'] = $name;
$item['price'] = $price;
$item['num'] = $num;

$this->items[$id] = $item;
}

/**
* 修改购物车中的商品数量
* @param $id 商品主键
* @param int $num 某个商品修改后的数量,即直接把某商品的数量改为$num
* @return boolean
*/
public function modNum($id,$num=1){
if(!$this->hasItem($id)){
return false;
}

$this->items[$id]['num'] = $num;
}

/**
* 商品数量增加1
* @param $id int 商品主键
* @param $num int
*/
public function incNum($id,$num=1){
if($this->hasItem($id)){
$this->items[$id]['num'] +=$num;
}
}

/**
* 商品数量增加1
* @param $id 商品主键
* @param $num
*/
public function decNum($id,$num=1){
if($this->hasItem($id)){
$this->items[$id]['num'] -=$num;
}

//如果减少后,数量为0了,则把这个商品从购物车删掉
if($this->items[$id]['num'] $this->delItem($id);
}
}

/**
*删除商品
* @param $id
*/
public function delItem($id){
unset($this->items[$id]);
}

/**
* 判断商品是否存在
* @param $id 商品主键
* @return bool
*/
public function hasItem($id){
return array_key_exists($id,$this->items);
}

/**
* 商品种类
* @return int
*/
public function getCnt(){
return count($this->items);
}

/**
* 查询购物车中商品的个数
* @return int
*/
public function getNum(){
if($this->getCnt()==0){
return 0;
}

$sum=0;
foreach($this->items as $item){
$sum +=$item['num'];
}

return $sum;
}

/**
* 查询购物车中商品的总金额
* @return float|int
*/
public function getPrice(){
if($this->getCnt()==0){
return 0;
}

$price=0.0;
foreach($this->items as $item){
$price +=$item['num'] * $item['price'];
}

return $price;
}

/**
* 返回购物车中的所有商品
*/
public function getAll(){
return $this->items;
}

/**
* 清空购物车
*/
public function clear(){
$this->items = array();
}
}

//调用方法
$cart = CartTool::getCart();
$cart->addItem(1,'电脑',120,1);
$cart->addItem(2,'手机',500,1);
$cart->addItem(3,'平板',900,1);
print_r($cart->getAll());

?>

AD:真正免费,域名+虚机+企业邮箱=0元

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
Previous article:3.2.1自动获取上传配置上传Next article:网贷相关