skip to content

PHP: Abstract Shopping Cart Class

 Tweet0 Shares0 Tweets

Once you have an evolving PHP codebase you will start to encounter cases where simple class inheritance is no longer workable. That's when more advanced language features can come into play, including interfaces, traits and abstract classes.

Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation.

Here we're documenting a simple abstract class on top of which you can build a shopping cart.

The AbstractShoppingCart class

The requirements for a shopping cart are fairly basic - we just need to record the contents of the user's shopping cart and allow for items to be added, removed, or quantities updated.

The thing is that, while using the same internal representation, the final implementation may use a COOKIE, SESSION, or some other kind or persistant storage.

Our abstract class then defines only the internal representation and basic functions, while leaving the storage-related methods as abstract:

<?PHP // Original PHP code by Chirp Internet: www.chirp.com.au // Please acknowledge use of this code by including this header. abstract class AbstractShoppingCart { protected $basket = []; abstract public function load(); abstract public function update(Array $data); abstract public function store(); public function get_basket() { return $this->basket; } public function add($productid, $sku, $qty) { $this->basket[$productid][$sku] = $qty; } public function remove($productid, $sku) { unset($this->basket[$productid][$sku]); } public function clear() { $this->basket = []; $this->store(); } } ?>

For our purposes each item in the cart consists of: ProductId, SKU and Qty, and these values are stored in an associative array. Your own requirements may be different.

At this stage our class is non-functional because it cannot be instantiated. Only static methods could be called, if there were any.

Extending the Abstract Class

We can now create a cookie-based shopping cart class by extending the abstract class and providing implementations for the three abstract methods: load(), update() and store():

<?PHP class ShoppingCartCookie extends AbstractShoppingCart { private $cookie = __CLASS__; public function __construct($cookie_name = false) { if($cookie_name) { $this->cookie = $cookie_name; } } public function load() { ... } public function update(Array $data) { ... } public function store(Array $cookie_params = []) { ... } } ?>

Without going into the implementation details, you can see that our ShoppingCartCookie will have access to the $basket variable and associated methods from AbstractShoppingCart while providing it's own functionality for reading and writing to the shopping cart cookie.

We could just as easily create a ShoppingCartSession class using the same approach. The only restriction is that you must implement all abstract methods and their implementation must match, more or less, the method's signature.

In PHP 5.4 and higher the cookie/session functionality can be implemented as traits. This gets around the limitation of not being able to inherit both our shopping cart abstract class and a cookie/session handling class simultaneously.

References

< PHP

Send a message to The Art of Web:


used only for us to reply, and to display your gravatar.

CAPTCHA

<- copy the digits from the image into this box

press <Esc> or click outside this box to close

Post your comment or question
top