Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntax error, unexpected 'Request' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) Laravel

Tags:

php

laravel

I have a taxi system, and this code gives me

<?php
namespace App\Controllers;

use Illuminate\Http\Request;

class Controller
{
    public Request $request;
    public array $user;

    public function __construct()
    {
        $this->user = [];
        $this->request = Request::capture();
        if (key_exists('user', $_SESSION)) {
            $this->user = $_SESSION['user'];
        }
        if (count($this->user) === 0 && url()->contains('panel')) {
            return redirect(url('login-form'));
        }
    }
}

but the system is giving me error after writing these lines of code

ParseError 
syntax error, unexpected 'Request' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
like image 534
Niklas Avatar asked Mar 06 '26 11:03

Niklas


2 Answers

Yeah, typed properties were introduced in PHP 7.4. In order to make your code snippet work, please remove properties type hints. Or update your PHP version to 7.4.

<?php

namespace App\Controllers;

use Illuminate\Http\Request;

class Controller
{
    public $request;
    public $user;

    public function __construct()
    {
        $this->user = [];
        $this->request = Request::capture();
        if (key_exists('user', $_SESSION)) {
            $this->user = $_SESSION['user'];
        }
        if (count($this->user) === 0 && url()->contains('panel')) {
            return redirect(url('login-form'));
        }
    }
}
like image 120
Mikhail Prosalov Avatar answered Mar 09 '26 03:03

Mikhail Prosalov


Typed properties were introduced in PHP 7.4: https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.typed-properties

If you are using an older PHP version, it will throw an error.

like image 26
Zoli Szabó Avatar answered Mar 09 '26 04:03

Zoli Szabó



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!