> Yes, this is a possible alternative interpretation of the readonly concept. > The current proposal is closer to how Java final variables work, which never > allow reassignment, while your suggestion is closer to C# readonly, which > does allow reassigning in the constructor. > > I went with the current proposal, because it integrates nicely with the > initialization concept introduced by typed properties. At the time, we also > had lots of discussions about how typed property initialization should work, > and requiring that all typed properties be initialized by the end of the > constructor was one of the contenders. However, we ultimately decided against > such an approach, because it does not integrate well with other aspects of > the language. In particular we need to have the ability to initialize an > object while bypassing the constructor, and we don't have the ability to > determine initialization statically, so an uninitialized state would have > been required in any case. > > If we had gone with that alternative initialization model at the time, then > we would also use the same one for readonly properties. But that's not how > things work now. Doing something like this now would require introducing an > entirely new scoping concept. This brings up awkward questions like: How do > you bind a closure to this new "initialization scope"? Closure rebinding is a > common way to bypass visibility restrictions. What happens if you want to > call a helper method to do the initialization, does that still count as > initialization scope? What if you call $object->__construct() directly, will > that allow changing properties? > > The current proposal tries to provide a simple, absolute guarantee: Once > initialized, the property will never change again. I think that makes the > feature easy to understand and reason about.
Thanks for the detailed explanation, I'm sold! And it seems it can evolve further once complementary features are introduced. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php