KevinM1 Posted December 8, 2006 Share Posted December 8, 2006 I've created a simple shopping cart class that's supposed to store items of a different class in an array. Unfortunately, I'm getting the following error in my tests:[quote]Parse error: syntax error, unexpected T_ARRAY, expecting T_STRING or T_VARIABLE or '$' in /home/thinkin8/php_classes/ShoppingCart.php on line 11[/quote]My class code in question is:[code]<?phpclass ShoppingCart extends Product{ private $Cart; public function addProduct($name, $price, $quantity){ $this -> Cart[] = new Product($name, $price, $quantity); } public function clearCart(){ $this -> Cart = new array(); } public function getTotal(){ $total = 0; foreach($this -> Cart as $value){ $total += $value -> getPrice(); } return $total; } public function showCart(){ echo "<table><tr><th>Name:</th><th>Quantity</th><th>Total Price</th></tr>\n"; foreach($this -> Cart as $value){ echo "<tr><td>$value->getName()</td><td>$value->getQuan()</td><td>\${$value->getPrice()}</td></tr>\n"; } }}?>[/code]The class it extends is:[code]<?phpclass Product{ private $Name; private $Price; private $Quantity; public function __construct($name, $price, $quantity){ $this -> Name = $name; $this -> Price = $price; $this -> Quantity = $quantity; } public function getName(){ return $this -> Name; } public function getQuan(){ return $this -> Quantity; } public function getPrice(){ return ($this -> Price) * ($this -> Quantity); } public function showAll(){ echo "{$this -> Name}<br />\n"; echo "\${$this -> Price}<br />\n"; echo "{$this -> Quantity}"; }}?>[/code]I'm using PHP 5.1.6, if that matters. Please help.Thanks :) Quote Link to comment https://forums.phpfreaks.com/topic/29948-oop-array-issue/ Share on other sites More sharing options...
onlyican Posted December 8, 2006 Share Posted December 8, 2006 new is when declaring a class in phpYou dont need new in php for array$this -> Cart = array(); Quote Link to comment https://forums.phpfreaks.com/topic/29948-oop-array-issue/#findComment-137601 Share on other sites More sharing options...
KevinM1 Posted December 8, 2006 Author Share Posted December 8, 2006 [quote author=onlyican link=topic=117877.msg481230#msg481230 date=1165600497]new is when declaring a class in phpYou dont need new in php for array$this -> Cart = array();[/quote]Thanks for the help! :) Quote Link to comment https://forums.phpfreaks.com/topic/29948-oop-array-issue/#findComment-137604 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.