On Fri, Mar 21, 2025 at 9:30 PM Daniel Scherzer <daniel.e.scher...@gmail.com>
wrote:

>
> On Fri, Mar 21, 2025 at 9:45 AM Alexandru Pătrănescu <dreal...@gmail.com>
> wrote:
>
>>
>> On Fri, Mar 21, 2025 at 5:20 PM Daniel Scherzer <
>> daniel.e.scher...@gmail.com> wrote:
>>
>>> On Fri, Mar 21, 2025 at 4:07 AM Tim Düsterhus <t...@bastelstu.be> wrote:
>>>
>>>> Can you clarify if the following would result in constructor property
>>>> promotion or not:
>>>>
>>>>      class Foo {
>>>>          public function __construct(
>>>>              final string $bar,
>>>>          ) { }
>>>>      }
>>>>
>>>> Best regards
>>>> Tim Düsterhus
>>>>
>>>
>>> Yes, that would result in constructor property promotion. I'll need to
>>> retarget the original PR for master, but at
>>> https://github.com/php/php-src/pull/17861 you can see in
>>> `Zend/tests/property_hooks/final_prop_promoted_2.phpt` a very similar test
>>> case.
>>>
>>>
>> I see. Good catch Tim.
>> Daniel, you don't have this exact test case there.
>>
>> Initially, in 8.0, only a visibility keyword would trigger the property
>> promotion logic.
>> Later, in 8.1, also the readonly keyword would trigger this and make the
>> property public, without the need to mention the visibility:
>> https://3v4l.org/Co0gl
>>
>> Now we want the same for the final keyword, to trigger the property
>> promotion without having a visibility keyword,
>> because like readonly, it would not be applicable to a parameter, but
>> just to a property?
>>
>> --
>> Alex
>>
>
> Sorry, I completely missed that part. Yes, that should trigger promotion,
> and if it doesn't then that is a bug in my implementation. When I retarget
> the PR to master I'll also add a test case for promotion from just using
> `final`.
>
>
Hi Daniel,

When discussing the Aviz RFC, I also wrote on this topic. (
https://news-web.php.net/php.internals/123488)
The main issue being that if we ever decide to use the final modifier for a
parameter, it would not be possible.
It is already now possible to use the readonly modifier for a parameter
because of this, something I wasn't aware of until this topic.

On the readonly doing the promotion by itself, I believe it was an internal
decision (that we properly documented later), but it was not discussed on
the mailing list as far as I know.
Also, I believe there might not be an actual test that validates this
behavior.

Having said that, allowing final to trigger property promotion by itself is
not such a problematic thing, IMO. I would have preferred forcing a
visibility keyword to trigger property promotions, but the cat is out of
the bag already.
Also, allowing the final modifier for constructor parameters to be
transformed as the final modifier for promoted properties is fine.

I guess that if we ever decide to use a keyword for declared variables or
parameters to disallow changing their values, we can go with some other
keyword like const, val, etc.

-- 
Alex

Reply via email to