Shopping Cart Class help
<p>I have written a Shopping cart Class, it use session to store data. I am not sure if I have implemented correctly, is this the correct way to add Option/Item and Extras?</p>

<p>Each item have a number of options with price</p>

<p>Each Option can have many extras or without extras</p>

<p><strong>For example:</strong> </p>

<p>Item 1 -> (Option ID 1)</p>

<p>Item 2 -> (Option ID 3, Option ID 4)</p>

<p>Option ID 4 => (Extras ID: 1,5,7,8)</p>

<pre><code>&lt;?php
session_start();

class Cart {

 private $cartName;

 public function __construct($cartName)
    {
        $this-&gt;cartName = $cartName;
        if (!isset($_SESSION[$this-&gt;cartName]))
        {
            $_SESSION[$this-&gt;cartName] = array();
        }
 }

   public  function addItem($itemid, $optionid)
     {
        if (array_key_exists($optionid, $_SESSION[$this-&gt;cartName]))
         {
            $_SESSION[$this-&gt;cartName][$optionid]['quantity']++;
            $_SESSION[$this-&gt;cartName]['LastUpdated']  = $optionid;
         }
            else
                {
                    $_SESSION[$this-&gt;cartName]['LastUpdated']  = $optionid;
                    $_SESSION[$this-&gt;cartName][$optionid] = array();
                    $_SESSION[$this-&gt;cartName][$optionid]['quantity'] = 1;
                    $_SESSION[$this-&gt;cartName][$optionid]['item_id'] = $itemid;
                    $_SESSION[$this-&gt;cartName][$optionid]['extras_id'] = array();
                }
     }

    public function addExtra($optionid,$extraid)
    {
        if (array_key_exists($optionid, $_SESSION[$this-&gt;cartName]))
            {
               if (!in_array($extraid, $_SESSION[$this-&gt;cartName][$optionid]['extras_id']))
               {
                   $_SESSION[$this-&gt;cartName][$optionid]['extras_id'][] = $extraid;
                }
            }
    }

}

$test =  new Cart("shopCart");

$test-&gt;addItem(5,2);
$test-&gt;addExtra(2,10);
$test-&gt;addExtra(2,20);
$test-&gt;addExtra(2,30);

$test-&gt;addItem(5,4);

echo "&lt;pre&gt;";
print_r($_SESSION);
echo "&lt;/pre&gt;";
</code></pre>
