Php shopping cart class implementation code (Singleton mode)

Source: Internet
Author: User
Php shopping cart class implementation code (Singleton mode)

  1. /**

  2. * Php shopping cart
  3. * Singleton mode
  4. * Edit bbs.it-home.org
  5. */
  6. Class Cart {
  7. Static protected $ ins; // instance variable
  8. Protected $ item = array (); // puts the commodity container

  9. // Disable external calls

  10. Final protected function _ construct (){
  11. }

  12. // Clone prohibited

  13. Final protected function _ clone (){
  14. }

  15. // Class internal instantiation

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

  20. Return self: $ ins;

  21. }

  22. // To save the product across pages, put the object into the 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. // Check whether the column is included in $ 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']; // return the number of items
  38. }
  39. }

  40. // Add a product

  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 (); // an item is an array

  47. $ This-> item [$ goods_id] ['num'] = $ num; // quantity of items purchased
  48. $ This-> item [$ goods_id] ['name'] = $ name; // item name
  49. $ This-> item [$ goods_id] ['price'] = $ price; // unit price
  50. }

  51. // Reduce a commodity

  52. Public function performanceitem ($ 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. // Remove a commodity

  63. Public function Delitem ($ goods_id ){
  64. If ($ this-> Initem ($ goods_id )){
  65. Unset ($ this-> item [$ goods_id]);
  66. }
  67. }

  68. // Return to the list of purchased items

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

  72. // Total number of items

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

  76. // Obtain the total number of items

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

  80. // Query the number of items in the shopping cart

  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. // Calculate the total price

  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. // Clear the shopping cart

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

Call example:

  1. Include_once ('cart. php ');
  2. $ Cart = Cart: Getcat ();
  3. $ Cart-> Additem ('1', 'php tutorial (Programmer's house Edition) ', '5', '123 ');
  4. Print_r ($ cart );
  5. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.