yivi
yivi

Reputation: 47646

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

I have updated my class definitions to make use of the newly introduced property type hints, like this:

class Foo {

    private int $id;
    private ?string $val;
    private DateTimeInterface $createdAt;
    private ?DateTimeInterface $updatedAt;

    public function __construct(int $id) {
        $this->id = $id;
    }


    public function getId(): int { return $this->id; }
    public function getVal(): ?string { return $this->val; }
    public function getCreatedAt(): ?DateTimeInterface { return $this->createdAt; }
    public function getUpdatedAt(): ?DateTimeInterface { return $this->updatedAt; }

    public function setVal(?string $val) { $this->val = $val; }
    public function setCreatedAt(DateTimeInterface $date) { $this->createdAt = $date; }
    public function setUpdatedAt(DateTimeInterface $date) { $this->updatedAt = $date; }
}

But when trying to save my entity on Doctrine I am getting an error saying:

Typed property must not be accessed before initialization

This not only happens with $id or $createdAt, but also happen with $value or $updatedAt, which are nullable properties.

Upvotes: 177

Views: 274530

Answers (3)

joas
joas

Reputation: 311

The solution has already been discussed in other answers. You just have to initialize the field.

public ?string $name = null;

But it remains a problem when updating records in a database from object models, because you may not need to update the entire record, and a field with a value of null can cause update errors, undesired results, and corrupted data in columns that accept NULL values ​​for other reasons, for example, a delete date.

Another solution is to check if the field is initialized via reflection. But the PHP reflection is infernally heavy, slow, and works badly.

You can also catch the error in a try block:

try {
    return $this->name;
}
catch(Error $e) {
    // $e->getCode() returns the name of the property that caused the error
    return null;
}

To get around all this complexity, I created a pseudotype called undefined so I can easily check if the field has been initialized (without reflection or mining the code with try blocks) and be able to use SQL NULL values ​​in conjunction with other types of data into data model objects. This idea works just like the Javascript undefined type does. Unfortunately it does not seem that PHP is going to adopt it at the moment, nor any other solution, but it is much more comfortable and secure than the rest of the solutions:

class undefined {
    private static $instance;
    public static function instance() { 
        return self::$instance ?? (self::$instance = new undefined()); 
    }
    private function __construct() {}
    public function __toString() {
        return 'undefined';
    }
}

// Curious PHP behavior where constants can be set as objects
define('undefined', undefined::instance());

// Our data object model
class User {
    public string|undefined $id = undefined;
    public string|undefined $name = undefined;
    public DateTime|null|undefined $deleted = undefined;
}

// When creating the model, all fields will be initialized as undefined.
$user = new User();

echo $user->deleted . PHP_EOL; // results: undefined

// Then we can check if the field is undefined or not
if($user->deleted !== undefined) {
    // Add field to the update command
    // ...
}
else {
    // Ignore field
    // ...
}

I hope this help.

Upvotes: -1

yivi
yivi

Reputation: 47646

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 in an undefined state, which will never match any declared type. undefined !== 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 null when 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 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.

For auto-generated IDs, the recommended way forward is to change the type declaration to:

private ?int $id = null

For all the rest, just choose an appropriate value for the property's type.

Upvotes: 283

Hhyperion
Hhyperion

Reputation: 772

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...

Upvotes: 50

Related Questions