3v4l.org

run code in 300+ PHP versions simultaneously
<?php class T { } class X { public function __construct(T $t) { $this->t = $t; } } class Builder { /** * @var T|null */ public $t; public function createX(): X { $this->validate(); // PHPStan complains that at this line, $this->t could be null, except we've already checked it isn't... // Parameter #1 $t of class X constructor expects T, T|null given. return new X($this->t); } private function validate() { if (is_null($this->t)) { throw new \Exception; } } } $builder = new Builder; $builder->t = new T; $builder->createX();
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Deprecated: Creation of dynamic property X::$t is deprecated in /in/WkL0Y on line 7
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33

preferences:
138.28 ms | 408 KiB | 5 Q