1

Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting end of file in C:\xampp\htdocs\example.php on line 9

I'm getting this error, but can't see nothing wrong with the code. What did I wrong? Won't return the expected results.

The program is supposed to show: echo $myorder->OrderTotal();

<?php
class CartEntry
{
//changing to private
private $Price;
private $Quantity;
}
//Adding functions to get price and quantity, 
public function __construct($Price, $Quantity)
{
$this->Price = $Price;
$this->Quantity = $Quantity;
}
public function ReturnPrice() {
return $this->Price;
}
public function ReturnQuantity() {
return $this->Quantity;
}
}
//
class CartContents
{
//Changed to private
private $items = array();
}
//Adding function to return items, same as above

public function __construct($items) {
$this->items = $items;
}
public function ReturnItems() {
return $this->items;
}
}


class Order
{
private $cart;
private $salesTax;

//cartcontents function removed
function __construct( float $salesTax, Array $items){


$this->salesTax = $salesTax;
$this->items = $items;

}


function OrderTotal()
{
$cartTotal = 0;
for ($i = 0; $i < count($this->items); $i++) {
$cartTotal += $this->items[$i]->Price * $this->items[$i]->Quantity;
}
$cartTotal += $cartTotal * $this->salesTax;
return $cartTotal;
}
}
$entry1 = new CartEntry();
$entry1->Price = 1.2;
$entry1->Quantity = 120;

$entry2 = new CartEntry();
$entry2->Price = 2.2;
$entry2->Quantity = 200;

$mycart = new CartContents();
$mycart->items = array($entry1, $entry2);
$items = $mycart->ReturnItems();
$mytax = 0.2;
//Items variable can be changed with mycart
$myorder = new Order($items, $mytax);
echo $myorder->OrderTotal();
?>

2 Answers2

0

You should put all your methods inside the class. In your code, the class CartEntry only contains 2 private variables:

class CartEntry
{
//changing to private
private $Price;
private $Quantity;
}

Anything outside the class will be considered invalid.

Seah Sky
  • 156
  • 1
  • 9
0

Here is the corrected code:

<?php
    class CartEntry
    {
    //changing to private
    private $Price;
    private $Quantity;

    public function __construct($Price, $Quantity)
    {
    $this->Price = $Price;
    $this->Quantity = $Quantity;
    }

    public function ReturnPrice() {
    return $this->Price;
    }
    public function ReturnQuantity() {
    return $this->Quantity;
    }
    }// end class

    class CartContents
    {
    //Changed to private
    private $items = array();

    public function __construct($items) {
    $this->items = $items;
    }
    public function ReturnItems() {
    return $this->items;
    }
    } // end class


    class Order
    {
    private $cart;
    private $salesTax;

    function __construct( float $salesTax, Array $items){

    $this->salesTax = $salesTax;
    $this->items = $items;
    }


    function OrderTotal()
    {
    $cartTotal = 0;
    for ($i=0, $max=count($this->items); $i < $max; $i++) {
    $cartTotal += $this->items[$i]->ReturnPrice() * $this->items[$i]->ReturnQuantity();
    }
    $cartTotal += $cartTotal * $this->salesTax;
    return $cartTotal;
    }
    }
    $entry1 = new CartEntry(1.2, 120);

    $entry2 = new CartEntry(2.2,200);

    $mycart = new CartContents([$entry1,$entry2]);
    $items = $mycart->ReturnItems();
    $mytax = 0.2;

    //Items variable can be changed with mycart
    $myorder = new Order($mytax,$items);
    echo $myorder->OrderTotal();

See live code.

One should avoid putting a brace after declaring class properties if the class has methods; that's what caused the error message that the OP encountered. As per the online Manual:

... class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods (emphasis mine) belonging to the class.

Don't let the term 'function' fool you. If it's in a class it is a method; see this discussion.

But after fixing that issue there were others.

If you are suppose to initialize properties in a constructor, then you need to pass in the correct parameters when you instantiate an object, instead of trying to set those properties as if they were public.

Note, also that when an object has methods for reading private properties then you need to use those methods instead of trying to access the private properties directly.

Lastly, I changed this line of code for ($i=0; $i < count($this->items); $i++) which executes correctly but it is more efficient to count the items just once instead of doing it on every iteration, so I inserted instead: for ($i=0, $max=count($this->items); $i < $max; $i++){

slevy1
  • 3,797
  • 2
  • 27
  • 33