close

[Solved] Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints? in php. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints Error Occurs?

Today I get the following error Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints? in php.

How To Solve Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints Error ?

  1. How To Solve Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints Error ?

    To Solve Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints Error For nullable typed properties you need to use syntax
    private ?string $val = null;

  2. Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints?

    To Solve Why I am suddenly getting a “Typed property must not be accessed before initialization” error when introducing properties type hints Error For nullable typed properties you need to use syntax
    private ?string $val = null;

Solution 1

Since PHP 7.4 introduces type-hinting for properties, it is particularly important to provide valid values for all properties, so that all properties have values that match their declared types.

A property that has never been assigned doesn’t have a null value, but it is on an undefined state, which will never match any declared typeundefined !== null.

For the code above, if you did:

$f = new Foo(1);
$f->getVal();

You would get:

Fatal error: Uncaught Error: Typed property Foo::$val must not be accessed before initialization

Since $val is neither string nor nullwhen accessing it.

The way to get around this is to assign values to all your properties that match the declared types. You can do this either as default values for the property or during construction, depending on your preference and the type of the property.

For example, for the above one could do:

class Foo {

    private int $id;
    private ?string $val = null; // <-- declaring default null value for the property
    private Collection $collection;
    private DateTimeInterface $createdAt;
    private ?DateTimeInterface $updatedAt;

    public function __construct(int $id) {
        // and on the constructor we set the default values for all the other 
        // properties, so now the instance is on a valid state
        $this->id = $id;
        $this->createdAt = new DateTimeImmutable();
        $this->updatedAt = new DateTimeImmutable();

        $this->collection = new ArrayCollection();
    }

Now all properties would have a valid value and the the instance would be on a valid state.

This can hit particularly often when you are relying on values that come from the DB for entity values. E.g. auto-generated IDs, or creation and/or updated values; which often are left as a DB concern.

Solution 2

For nullable typed properties you need to use syntax

private ?string $val = null;

otherwise it throws a fatal error.

Since this concept leads to unnecessary fatal errors, I have created a bug report https://bugs.php.net/bug.php?id=79620 – with no success, but at least I tried…

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read

Leave a Comment