首頁 > php教程 > PHP源码 > 商城购物车以PHP单例模式实现。

商城购物车以PHP单例模式实现。

PHP中文网
發布: 2016-05-25 17:10:47
原創
1097 人瀏覽過

1. [代码][PHP]代码   

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

<?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[&#39;cat&#39;]) || !($_SESSION[&#39;cat&#39;] instanceof self)){

            $_SESSION[&#39;cat&#39;] = self::Getins();

        }

        return $_SESSION[&#39;cat&#39;];

    }

 

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

        }

    }

 

    //添加一个商品

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

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

            $this->item[$goods_id][&#39;num&#39;] += $num;

            return;

        }

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

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

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

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

    }

 

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

    public function Getnumber(){

        $num = 0;

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

            return 0;

        }

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

            $num += $v[&#39;num&#39;];

        }

 

        return $num;

    }

 

    //计算总价格

    public function Getprice(){

        $price = 0;

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

            return 0;

        }

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

            $price += $v[&#39;num&#39;]*$v[&#39;num&#39;];

        }

        return $price;

    }

 

    //清空购物车

    public function Emptyitem(){

        $this->item = array();

    }

}

/*

     自己测试代码也拿出来

*/

?php

include_once(&#39;Cart.php&#39;);

$cart = Cart::Getcat();

$cart->Additem(&#39;1&#39;,&#39;谍匪&#39;,&#39;5&#39;,&#39;9999&#39;);

print_r($cart);

登入後複製

                   

                   

相關標籤:
php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
怎麼學好php
來自於 1970-01-01 08:00:00
0
0
0
PHP擴充intl
來自於 1970-01-01 08:00:00
0
0
0
php數據獲取?
來自於 1970-01-01 08:00:00
0
0
0
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板