3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** @template T of string|null */ class StringOrNull { /** @var T */ public ?string $value; /** @param T $value */ public function __construct(?string $value) { $this->value = $value; } /** * @template T2 of string|int * @param T2 $value * @psalm-this-out self<T2> */ public function setFromStringOrInt(string|int $value): void { if (is_string($value)) { // This is a separate issue, probably https://github.com/vimeo/psalm/issues/7739 $this->value = $value; } } } class Foo { /** @var list<StringOrNull<null>> */ private array $nulls = []; /** @param list<StringOrNull<null>> $nulls */ public function __construct(array $nulls) { $this->nulls = $nulls; } public function assertAllNull(): void { foreach ($this->nulls as $null) { assert($null->value === null); } } } $stringOrNull = new StringOrNull(null); $foo = new Foo([$stringOrNull]); $stringOrNull->setFromStringOrInt("foobar"); $foo->assertAllNull();

preferences:
21.1 ms | 404 KiB | 5 Q