Typed Property Not Initialized

Fatal error: Uncaught Error: Typed property App\Entity\User::$email must not be accessed before initialization

Quick Answer

You are accessing a typed property before it has been assigned a value. Initialize the property in the constructor or give it a default value in the declaration.

Why This Happens

PHP 7.4+ typed properties have an uninitialized state that is different from null. Accessing a typed property before it has been assigned a value throws this error. Unlike untyped properties which default to null, typed properties must be explicitly initialized.

The Problem

class User {
    public string $name;
    public string $email;
}
$user = new User();
echo $user->email; // Not initialized

The Fix

class User {
    public string $name = '';
    public string $email = '';
}
// Or initialize in constructor:
class User {
    public function __construct(
        public string $name,
        public string $email
    ) {}
}

Step-by-Step Fix

  1. 1

    Find the uninitialized property

    Check which property the error references and verify it has no default value and is not set in the constructor.

  2. 2

    Add a default value

    Give the property a default value in its declaration, such as public string $name = ''.

  3. 3

    Initialize in constructor

    Use constructor promotion or assign the property in __construct to ensure it is always initialized when the object is created.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free