[php-src] Issue #19173: Readonly Properties Cannot Have Default Values

From: Date: Fri, 18 Jul 2025 13:58:31 +0000
Subject: [php-src] Issue #19173: Readonly Properties Cannot Have Default Values
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-250452@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/19173 Author: adrian-enspired ### Description The following code: ```php <?php class X { public readonly array $a = []; } var_dump(new X()->a); class Y { public function __construct( public readonly array $a = [] ) {} } var_dump(new Y()->a); ``` The X example resulted in this output: ``` Fatal error: Readonly property X::$a cannot have default value ... ``` But I expected it to behave similarly / identically to the Y example: ``` array(0) { } ``` Reading the documentation, I do understand that the result I expected initially - i.e., to behave like an argument default value - was misguided. However, I see no reason for a default to be _disallowed_ simply because it would be (in the words of the manual) "essentially the same as a constant" and "not particularly useful." The behavior is inconsistent and unexpected. Ideally (in my mind, at least) the default value would be assigned if the property was accessed before being initialized, or after construct if it hadn't yet been initialized. But I recognize this would be a larger change in behavior and likely much more complicated to implement. ### PHP Version ```plain PHP 8.4.10 ``` ### Operating System _No response_

« previous php.bugs (#250452) next »