Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.