<?php declare(strict_types=1); final class SomeStringable { public function __toString(): string { echo "__toString executed" . PHP_EOL; return "foo"; } } final class SomeClass { public function __construct(public ?string $value) {} } $nullableVar = new SomeStringable(1); //$nullableVar = null; //$result = new SomeClass(null === $nullableVar ? null : (string) $nullableVar); // works $result = new SomeClass($nullableVar); // Fatal error var_dump($result->value);
You have javascript disabled. You will not be able to edit any code.