12

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)
3
  • 1
    Which version of PHP do you use? Commented Dec 10, 2020 at 21:52
  • 7.3 is my current version Commented Dec 10, 2020 at 22:00
  • 1
    I am using php 8 and still go the error somehow.... Commented Jul 7, 2022 at 20:23

2 Answers 2

18

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'));
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

hello, i uppdate the system to 7.4, works now :) thanks
7

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.