search
Homephp教程php手册单例模式实现购物车

采用单例模式写的购物车
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),