OO shopping cart class + product class interaction I built a small store site for learning purposes. I loaded my 'products' from the db into a Products class.  I used a method from this class to display the products in HTML.  Of course, the HTML contains 'Add to Cart' buttons which POST the product ID and quantity when a button is clicked.  The result is that the POSTed ID and quantity are sent to the Cart class.  This works, but it seems like I should be sending a product object to my cart instead of just two values that will be stored in an array.

I'm not sure how to do that, given that product information needs to be POSTed non-objectively.  And I'm not sure if it would be better.

class Products
{

private $products; 

function __construct($data)
{

    $this-&gt;products = $data;

}   

public function showProduct($id, $price, $type)
{
        echo "&lt;div id='$id' class='product'&gt;";
        echo $type."&lt;br/&gt;";
        echo $price."&lt;br/&gt;";            
        echo "Quantity: &lt;input class='quantity' name='quantity' value='1' type='text'/&gt;&lt;br/&gt;";
        echo "&lt;button onclick='add($id);'&gt;Add to Cart&lt;/button&gt;";
        echo "&lt;/div&gt;";      
}

public function showAll()
{
    foreach ($this-&gt;products as $id =&gt; $product) {

        $this-&gt;showProduct($id, $product-&gt;price, $product-&gt;type);
    }

}   



}


And the Cart class:

class Cart
{
public $items = array();

function __construct() {

    if (isset($_SESSION['cart'])) { $this-&gt;items = $_SESSION['cart']; } else { $_SESSION['cart'] = ''; }
}

private function update() {

    $_SESSION['cart'] = $this-&gt;items;
}   

public function add($id, $quantity) {

    $this-&gt;items[] = array('id'=&gt;"$id", 'quantity'=&gt;"$quantity");
    $this-&gt;update();

}
}


I'm not sure that I was able to get the objects from these classes to interact with each other properly.

Also, in this situation, since I need to use $_SESSION to keep the cart inventory persistent, I'm not sure why I need a Cart class at all.  It seems a little superfluous, but I have a nagging feeling that it shouldn't be.
I would have done it in the following way:

&lt;?php
    class Product {
        private $id;
        private $type;
        private $price;


        public function __toString() {
            return  "your way to display the Product object ie: &lt;p&gt; The id is: $this-&gt;getId &lt;/p&gt; ";
        }


        public function getId()
        {
            return $this-&gt;id;
        }


        public function setId($id)
        {
            $this-&gt;id = $id;
        }

        public function getType()
        {
            return $this-&gt;type;
        }

        public function setType($type)
        {
            $this-&gt;type = $type;
        }

        public function getPrice()
        {
            return $this-&gt;price;
        }

        public function setPrice($price)
        {
            $this-&gt;price = $price;
        }

    }

    class Cart {
        private $products;
        private $id;

        public function getProducts()
        {
            return $this-&gt;products;
        }


        public function addProduct(Product $product)
        {
            $this-&gt;products[] = $product;
        }


        public function getId()
        {
            return $this-&gt;id;
        }


        public function setId($id)
        {
            $this-&gt;id = $id;
        }

        /**
         * Returns the sum off all products prices
         * array_reduce docs: http://php.net/manual/en/function.array-reduce.php
         */
        public function total(){
            return array_reduce($this-&gt;getProducts(), function($carry, $product){
                return $carry + $product-&gt;getPrice();
            });
        }

    }


It's important to note here that you are managing objects, so you will need some Object Relational Mapper to persist objects on a DB, such as Doctrine.

You need to separate concerns in a MVC architecture, the logic of your domain the 'M' of MVC (in this case the shopping cart) reside in a object model (Product + cart) . Then you need the 'C' here you tell the cart that you want to add a new product:

class Controller {
        public function someAction(){
            $product = new Product();
            $product-&gt;setId(1);
            $product-&gt;setPrice(100.00);
            $product-&gt;setType(3); //Id of the type

            $cart = new Cart;
            $cart-&gt;addProduct($product);

            $cart-&gt;total(); //Will return 100.00
            //Logic to obtain twig engine
            return $twig-&gt;render('Template', ['cart' =&gt; $cart]); //returns HTML
        }
    }


And finally the V of the MVC there is where you build your HTML:

Using a template engine like Twig:

     &lt;html&gt;
        &lt;body&gt;
        Total: {{cart.total()}}
           &lt;ol&gt;
              {% for product in cart.products %}
                 &lt;li&gt;{{product}}&lt;/li&gt;
              {% else %}
                 &lt;li&gt;Empty cart&lt;/li&gt;
              {% endfor %}
           &lt;/ol&gt;
        &lt;/body&gt;
    &lt;/html&gt;


Of course building this architecture on your own could be really complicated there is when frameworks like symfony kick in http://symfony.com/. It's just a personal recommendation.

Also, take a look at OOP patterns. They will make your life easier and you will learn how to program good architectures for models.