Php shopping cart class implementation code (Singleton mode)
/**
- * Php shopping cart
- * Singleton mode
- * Edit bbs.it-home.org
- */
- Class Cart {
- Static protected $ ins; // instance variable
- Protected $ item = array (); // puts the commodity container
// Disable external calls
- Final protected function _ construct (){
- }
// Clone prohibited
- Final protected function _ clone (){
- }
// Class internal instantiation
- Static protected function Getins (){
- If (! (Self: $ ins instanceof self )){
- Self: $ ins = new self ();
- }
Return self: $ ins;
- }
// To save the product across pages, put the object into the session
- Public function Getcat (){
- If (! ($ _ SESSION ['cat']) |! ($ _ SESSION ['cat'] instanceof self )){
- $ _ SESSION ['cat'] = self: Getins ();
- }
Return $ _ SESSION ['cat'];
- }
// Check whether the column is included in $ 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] ['num']; // return the number of items
- }
- }
// Add a product
- 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 (); // an item is an array
- $ This-> item [$ goods_id] ['num'] = $ num; // quantity of items purchased
- $ This-> item [$ goods_id] ['name'] = $ name; // item name
- $ This-> item [$ goods_id] ['price'] = $ price; // unit price
- }
// Reduce a commodity
- Public function performanceitem ($ 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;
- }
- }
// Remove a commodity
- Public function Delitem ($ goods_id ){
- If ($ this-> Initem ($ goods_id )){
- Unset ($ this-> item [$ goods_id]);
- }
- }
// Return to the list of purchased items
- Public function Itemlist (){
- Return $ this-> item;
- }
// Total number of items
- Public function Gettype (){
- Return count ($ this-> item );
- }
// Obtain the total number of items
- Public function Getunm ($ goods_id ){
- Return $ this-> item [$ goods_id] ['num'];
- }
// Query the number of items in the shopping cart
- Public function Getnumber (){
- $ Num = 0;
- If ($ this-> Gettype () = 0 ){
- Return 0;
- }
Foreach ($ this-> item as $ k => $ v ){
- $ Num + = $ v ['num'];
- }
- Return $ num;
- }
// Calculate the total price
- Public function Getprice (){
- $ Price = 0;
- If ($ this-> Gettype () = 0 ){
- Return 0;
- }
Foreach ($ this-> item as $ k => $ v ){
- $ Price + = $ v ['num'] * $ v ['num'];
- }
- Return $ price;
- }
// Clear the shopping cart
- Public function Emptyitem (){
- $ This-> item = array ();
- }
- }
- ?>
Call example:
- Include_once ('cart. php ');
- $ Cart = Cart: Getcat ();
- $ Cart-> Additem ('1', 'php tutorial (Programmer's house Edition) ', '5', '123 ');
- Print_r ($ cart );
- ?>
|