php 購物車類的實現代碼(單例模式)

來源:互聯網
上載者:User
  1. /**

  2. * php 購物車類
  3. * 單例模式
  4. * Edit bbs.it-home.org
  5. */
  6. class Cart{
  7. static protected $ins; //執行個體變數
  8. protected $item = array(); //放商品容器

  9. //禁止外部調用

  10. final protected function __construct(){
  11. }

  12. //禁止複製

  13. final protected function __clone(){
  14. }

  15. //類內部執行個體化

  16. static protected function Getins(){
  17. if(!(self::$ins instanceof self)){
  18. self::$ins = new self();
  19. }

  20. return self::$ins;

  21. }

  22. //為了能使商品跨頁面儲存,把對象放入session裡

  23. public function Getcat(){
  24. if(!($_SESSION['cat']) || !($_SESSION['cat'] instanceof self)){
  25. $_SESSION['cat'] = self::Getins();
  26. }

  27. return $_SESSION['cat'];

  28. }

  29. //入列時的檢驗,是否在$item裡存在.

  30. public function Initem($goods_id){
  31. if($this->Gettype() == 0){
  32. return false;
  33. }
  34. if(!(array_key_exists($goods_id,$this->item))){
  35. return false;
  36. }else{
  37. return $this->item[$goods_id]['num']; //返回此商品個數
  38. }
  39. }

  40. //添加一個商品

  41. public function Additem($goods_id,$name,$num,$price){
  42. if($this->Initem($goods_id) != false){
  43. $this->item[$goods_id]['num'] += $num;
  44. return;
  45. }

  46. $this->item[$goods_id] = array(); //一個商品為一個數組

  47. $this->item[$goods_id]['num'] = $num; //這一個商品的購買數量
  48. $this->item[$goods_id]['name'] = $name; //商品名字
  49. $this->item[$goods_id]['price'] = $price; //商品單價
  50. }

  51. //減少一個商品

  52. public function Reduceitem($goods_id,$num){
  53. if($this->Initem($goods_id) == false){
  54. return;
  55. }
  56. if($num > $this->Getunm($goods_id)){
  57. unset($this->item[$goods_id]);
  58. }else{
  59. $this->item[$goods_id]['num'] -=$num;
  60. }
  61. }

  62. //去掉一個商品

  63. public function Delitem($goods_id){
  64. if($this->Initem($goods_id)){
  65. unset($this->item[$goods_id]);
  66. }
  67. }

  68. //返回購買商品列表

  69. public function Itemlist(){
  70. return $this->item;
  71. }

  72. //一共有多少種商品

  73. public function Gettype(){
  74. return count($this->item);
  75. }

  76. //獲得一種商品的總個數

  77. public function Getunm($goods_id){
  78. return $this->item[$goods_id]['num'];
  79. }

  80. // 查詢購物車中有多少個商品

  81. public function Getnumber(){
  82. $num = 0;
  83. if($this->Gettype() == 0){
  84. return 0;
  85. }

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

  87. $num += $v['num'];
  88. }
  89. return $num;
  90. }

  91. //計算總價格

  92. public function Getprice(){
  93. $price = 0;
  94. if($this->Gettype() == 0){
  95. return 0;
  96. }

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

  98. $price += $v['num']*$v['num'];
  99. }
  100. return $price;
  101. }

  102. //清空購物車

  103. public function Emptyitem(){
  104. $this->item = array();
  105. }
  106. }
  107. ?>

複製代碼

調用樣本:

  1. include_once('Cart.php');
  2. $cart = Cart::Getcat();
  3. $cart->Additem('1','php學習教程(程式員之家版)','5','9999');
  4. print_r($cart);
  5. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.