Jump to content

OOP array issue


KevinM1

Recommended Posts

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]
<?php

class 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]
<?php

class 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 :)
Link to comment
https://forums.phpfreaks.com/topic/29948-oop-array-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.