Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > PHP/MySQL/Scripting

  • Print
  • + Share This
From the author of The Item Class

The Item Class

With all of the shopping cart functionality now reliably implemented, an Item class is required. As already said, the only requirement is that it has a getId() method, to be used by the ShoppingCart class:

class Item {
    protected $id;
    public function getId() {
        return $this->id;

Again, the class uses a protected attribute for the ID, with the expectation that the class will be extended by you on a site-by-site basis. (Alternatively, you could define Item as an abstract class and force the developer to create extensions of it.)

The rest of the class code could be similar to that, handling the item’s name, price, and whatever other properties are appropriate. All of that data would be passed to the constructor when Item objects are created (see the downloadable file for the complete code).

  • + Share This
  • 🔖 Save To Your Account