Re: Constructor promoted property and default value
| From: | Pierre | Date: | Mon, 23 Oct 2023 16:22:45 +0000 |
| Subject: | Re: Constructor promoted property and default value | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-121450@lists.php.net to get a copy of this message | ||
Le 23/10/2023 à 18:11, Saki Takamachi a écrit :
Which then would simply be the same behavior as properties when not promoted but declared in the class body instead:If I understand your use case properly, you should be confused by properties with default values that are not constructor-promoted as well ? Am I wrong ? In this case, your problem is not with promoted properties ?If we specify it the way you say, the initial values of the constructor arguments will be available even when the constructor is not called. Such behavior felt a little counterintuitive.
class Foo
{
public $val = 'abc';
}
$redis_foo = serialize(new Foo());
$foo = unserialize($redis_foo);
var_dump($foo->val);
// string(3) "abc"
Right ?
What's the most disturbing in my opinion is that: `class Foo { public string $val = 'abc' } and class Foo { public function __construct(public string $val = 'abc' ) {}}` don't yield the same behavior at the time.
Regards,
--
Pierre