Reputation: 2686
I am using php8.2 in a Symfony 6.4 app. I have a DTO, which is defined as follows:
final class Store
{
public function __construct(
public readonly string $storeNumber,
public readonly bool $central,
public readonly string $name,
public readonly ?string $city = null,
public readonly ?string $zip = null,
public readonly ?string $street = null,
public readonly ?bool $published = null,
) {
}
}
The only time the constructor is called is in a method, where I match a stdClass object to this class:
class StoreHelper
{
public static function mapFromFetchedData(stdClass $store): Store
{
$city = ($store->address && $store->address->city) ? $store->address->city : null;
$zipCode = ($store->address && $store->address->zipCode) ? $store->address->zipCode : null;
$street = ($store->address && $store->address->street) ? $store->address->street : null;
$published = ($store->published) ?: null;
return new Store(
storeNumber: $store->number,
central: $store->central,
name: $store->name,
city: $city,
zip: $zipCode,
street: $street,
published: $published,
);
}
}
Locally this runs fine (nice!), but on the production server I get this error and I have no clue why and what I can do against it:
Error thrown while running command "'app:personal-sub'". Message: "Typed property App\Store\Store::$published must not be accessed before initialization"
This happens when I run a command on the console, which at some point calls this class.
Originally I had not set the null default values, so I tried that and it made no difference at all. As I understand it setting all properties in the constructor should avoid this problem even if the value is only null, when it is set.
How can I avoid this error?
Upvotes: 0
Views: 6