<?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
;
}
public
function
Getcat(){
if
(!(
$_SESSION
['cat']) || !(
$_SESSION
['cat']
instanceof
self)){
$_SESSION
['cat'] = self::Getins();
}
return
$_SESSION
['cat'];
}
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
);