Php, typed property and lazy loading? How can I check if it’s initialized?

you know Php 7.2 has typed properties:

class Test { }

class Test2
{
    private Test $test;

    public function __construct()
    {
        $this->test = new Test();
    }
}

so far so good, but what if I want to have a lazy created object?

public function createIfNotExists()
{
    if ($this->test === null) // *ERROR
    {
    }
}

this fails:

Typed property must not be accessed before initialization

but I want to check either it's been created, not using it. How to?



Read more here: https://stackoverflow.com/questions/67392337/php-typed-property-and-lazy-loading-how-can-i-check-if-its-initialized

Content Attribution

This content was originally published by john smith at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: