dongli564510 2019-03-31 05:02
浏览 65
已采纳

在简单的PHP代码上发现错误,“解析错误”[重复]

This question already has an answer here:

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();
?>
</div>
  • 写回答

2条回答 默认 最新

  • dongliulu1122 2019-03-31 07:05
    关注

    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++){

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值