3v4l.org

run code in 300+ PHP versions simultaneously
<?php class WithPromotedProps { public function __construct( private readonly string $data = '', ) {} } // Value is initialized with empty string var_dump(new WithPromotedProps()); $ref = new ReflectionClass(WithPromotedProps::class); $instance = $ref->newInstanceWithoutConstructor(); // Value isn't initialized even with default value var_dump($instance); // // // // Fatal error because class with promoted props can't have default value: /* class GivesFatalError { private readonly string $data = ''; } */ // // // class WithoutPromotedProps { private string $data = ''; public function __construct(string $data = '') { $this->data = $data; } } $ref = new ReflectionClass(WithoutPromotedProps::class); $instance2 = $ref->newInstanceWithoutConstructor(); // Value is initialized with empty string var_dump($instance2); // // // class WithPromotedPropsButNotReadonly { public function __construct( private string $data = '', ) {} } $ref = new ReflectionClass(WithPromotedPropsButNotReadonly::class); $instance3 = $ref->newInstanceWithoutConstructor(); // Value isn't initialized even with default value var_dump($instance3);
Output for 8.1.32, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
object(WithPromotedProps)#1 (1) { ["data":"WithPromotedProps":private]=> string(0) "" } object(WithPromotedProps)#2 (0) { ["data":"WithPromotedProps":private]=> uninitialized(string) } object(WithoutPromotedProps)#1 (1) { ["data":"WithoutPromotedProps":private]=> string(0) "" } object(WithPromotedPropsButNotReadonly)#3 (0) { ["data":"WithPromotedPropsButNotReadonly":private]=> uninitialized(string) }

preferences:
94.38 ms | 407 KiB | 5 Q