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();
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
Fatal error: Uncaught AssertionError: assert($null->value === null) in /in/mKN4o:41 Stack trace: #0 /in/mKN4o(41): assert(false, 'assert($null->v...') #1 /in/mKN4o(49): Foo->assertAllNull() #2 {main} thrown in /in/mKN4o on line 41
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE) in /in/mKN4o on line 19
Process exited with code 255.

preferences:
67.32 ms | 407 KiB | 5 Q