laravel 解析错误:语法错误,意外的“__construct”(T_STRING),需要函数(T_FUNCTION)或常量(T_CONST)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53881476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse error: syntax error, unexpected '__construct' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
提问by Angelika Beňová
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)
解析错误:语法错误,意外的“__construct”(T_STRING),需要函数(T_FUNCTION)或常量(T_CONST)
I'm new in Laravel and I really have no idea what I have to do,
我是 Laravel 的新手,我真的不知道我必须做什么,
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:
这是我在 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
这是我的 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;
}
}
回答by Udhav Sarvaiya
回答by Ahmed Nour Jamal El-Din
there is missing functionkeyword before __construct
__construct 之前缺少function关键字
<?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;
}
}
回答by mrhn
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,当通过 this 访问变量时,不需要在访问的变量(在本例中为 items)上做 $ 符号,所以它应该总是:
$this->items[$id] = $storedItem;
And please indent and organise your code better, this is pretty chaotic and there is probably more than one error.
并且请更好地缩进和组织您的代码,这非常混乱,并且可能有不止一个错误。
回答by mrhn
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;
}
}