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);

preferences:
30.49 ms | 404 KiB | 5 Q