Parse error: syntax error, unexpected '__construct' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)

点点圈 提交于 2019-12-11 08:57:18

问题


I have this problem. When I click on add to cart button there is an error:

Parse error: syntax error, unexpected '__construct' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)

I'm new in Laravel and I really have no idea what I have to do,

This is my code on button add to Cart:

<a href="{{route('get.addToCart',[$product->id])}}" class="cart-btn">Add to cart</a>

This is my route:

Route::get('add-to-cart/{id}', 'WebController@addToCart')->name('get.addToCart');

This I have in webcontroller:

public function addToCart(Request $request, $id){
   $product = Product::find($id);
   $oldCart = Session::has('cart') ? Session::get('cart') : null;
   $cart = new Cart($oldCart);
   $cart->add($product,$product->id);
   $request->session()->put('cart',$cart);
   dd($request->session()->get('cart'));
   return redirect()->route(get.product);
}

And this is my cart.php

<?php
namespace App;
class Cart{
   public $items=null;
   public __construct($oldCart){
      if($oldCart){
        this->$items=$oldCart->items;
      }
   }
   public function add($item,$id){
      $storedItem= ['name'=>$item];
      if(this-> $items)
      { 
         if(array_key_exists($id, this->$items)){
           $storedItem=this->$items[$id];
         }
      }
      this->$items[$id]=$storedItem;
   }
}

回答1:


Your __construct needs to have the word function in front of it, or better yet public function

Change the line to:

public function __construct($oldCart)

Read out here: Line no 8




回答2:


there is missing function keyword before __construct

<?php
namespace App;

class Cart{
    public $items=null;
    public function __construct($oldCart){
        if($oldCart){
            $this->items=$oldCart->items;
        }
    }

    public function add($item,$id){
        $storedItem= ['name'=>$item];
        if($this->items)
            {
                if(array_key_exists($id, $this->items)){
                     $storedItem=$this->items[$id];
                }
            }
        $this->items[$id]=$storedItem;
    }
}



回答3:


this->$items[$id]=$storedItem;

See, this is a keyword and is $this, and when accessing variables through this no need to do the $ sign on the accessed variable (in this case items), so it should always be:

$this->items[$id] = $storedItem;

And please indent and organise your code better, this is pretty chaotic and there is probably more than one error.




回答4:


You should try this:

<?php
namespace App;
class Cart{
public $items=null;
public function __construct($oldCart){
    if($oldCart){
        this->$items=$oldCart->items;
    }
}
public function add($item,$id){
    $storedItem= ['name'=>$item];
    if(this-> $items)
        {if(array_key_exists($id, this->$items)){
            $storedItem=this->$items[$id];
        }
        }
        this->$items[$id]=$storedItem;
}
}


来源:https://stackoverflow.com/questions/53881476/parse-error-syntax-error-unexpected-construct-t-string-expecting-functi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!