Parse error: syntax error, unexpected '__construct' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Resolving Parse Error: Syntax Error Unexpected '__construct' When Using Laravel Add to Cart Functionality
Introduction
Parse errors occur when there is a problem in the syntax of your code, which prevents PHP from correctly interpreting and executing your instructions. These issues are often related to missing semicolons, incorrect variable usage, or mismatched quotes. In this blog post, we will focus on resolving a specific issue that arises when adding products to a cart using Laravel.
The issue described in the introduction refers to an unexpected '__construct' error while clicking an add to cart button. This error is usually caused by issues with class instantiation and initialization, particularly with classes that are part of your application. To understand and solve this problem, let us take a close look at the code involved in this scenario.
Analyzing the Code
Your code consists of multiple elements, including a route declaration, a controller method, and a 'cart' class for handling shopping cart data. Let's break down each part:
1. Route declaration:
Route::get('add-to-cart/{id}', 'WebController@addToCart')->name('get.addToCart');
2. Controller method (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);
}
3. 'Cart' class:
<?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;
}
}
The first two parts seem to be working as expected, but the 'Cart' class might need some tweaking. The issue is likely caused by the `__construct()` method and how it handles the initialization of your shopping cart data. Let us address this problem with a better implementation.
Revisiting the Issue: Fixing the Code
To resolve the error, we will make the following changes to the 'Cart' class:
1. Remove the `__construct()` method from your Cart class (as it is currently unnecessary):
<?php
namespace App;
class Cart{
public $items=null;
}
2. Modify your WebController@addToCart method to instantiate the 'Cart' class:
public function addToCart(Request $request, $id){
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
// Replace with the updated code:
$cart = new Cart();
if($oldCart){
$cart->items=$oldCart->items;
}
$cart->add($product,$product->id);
$request->session()->put('cart',$cart);
dd($request->session()->get('cart'));
return redirect()->route(get.product);
}
Conclusion
In conclusion, the issue you faced was caused by an incorrect usage of the 'Cart' class constructor in your Laravel application. By removing the unnecessary `__construct()` method and making appropriate changes to your WebController@addToCart method, you have successfully resolved the parse error and can continue using the add-to-cart functionality without any issues. For more information on working with sessions and cart functionality in Laravel, refer to our blog or visit https://laravelcompany.com for comprehensive resources.