On Fri, Mar 19, 2021 at 12:57 PM Nikita Popov <nikita....@gmail.com> wrote:

> On Fri, Mar 19, 2021 at 12:22 PM Nikita Popov <nikita....@gmail.com>
> wrote:
>
>> On Wed, Mar 3, 2021 at 7:04 PM Alexandru Pătrănescu <dreal...@gmail.com>
>> wrote:
>>
>>>
>>> On Wed, Mar 3, 2021 at 5:49 PM Nikita Popov <nikita....@gmail.com>
>>> wrote:
>>>
>>>> On Wed, Mar 3, 2021 at 4:28 PM Alexandru Pătrănescu <dreal...@gmail.com>
>>>> wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> This looks very nice and I'm interested in further steps where not
>>>>> only new can be used :).
>>>>>
>>>>> The only thing I think it would be good to improve is to have a
>>>>> deterministic order for running initialization.
>>>>> Yes, this can be done at a later point, I guess. But maybe there is
>>>>> already an order of initialization right now and people would start
>>>>> replying on it and it would be good to mention it.
>>>>> Or maybe I didn't understand what this refers to: "this is not
>>>>> guaranteed behavior, and code should not rely on a specific point of
>>>>> evaluation."
>>>>>
>>>>
>>>> Which particular cases would you like to see specified? There are five
>>>> cases that have clearly defined behavior, and that I could explicitly
>>>> specify if desired:
>>>>
>>>>  * Non-class constants: Are evaluated immediately when declared (i.e.
>>>> when control flow reaches the declaration).
>>>>  * Attribute arguments: Are evaluated in the order of the arguments.
>>>>  * Parameter defaults: Are evaluated in the order of the parameters.
>>>>  * Non-static property defaults: Are evaluated in order of declaration,
>>>> with parent properties first. The constructor is run after defaults are
>>>> evaluated.
>>>>  * Static variables: Are evaluated immediately when declared (i.e. when
>>>> control flow reaches the declaration).
>>>>
>>>> And then there are the two problematic cases: Class constants and
>>>> static properties. Currently, PHP evaluates these semi-lazily. All class
>>>> constants and static properties are evaluated at the same time, on first
>>>> "use" of the class. I would consider this to be something of an
>>>> implementation detail. That's what I meant by that sentence.
>>>>
>>>> Now, if we allow "new" expressions, then I could see an argument in
>>>> favor of requiring class constant and static property initializers to be
>>>> evaluated eagerly, i.e. directly after the class has been declared. This
>>>> would be a (minor) backwards-compatibility break, because invalid
>>>> constant/property declarations would error out immediately, even if they
>>>> don't get used. However, I do think that this would be the most predictable
>>>> behavior once potentially side-effecting expressions are involved (we
>>>> already support side-effecting expressions right now, but less explicitly).
>>>>
>>>>
>>> Yes, this is what I was thinking about, to have a clear stated order of
>>> initialization.
>>> Yes, I agree that class constants and static properties should be
>>> eagerly declared when class is declared.
>>>
>>> So the order would be:
>>> - constants and static variables, when reaching the statement that does
>>> the declaration
>>> - class constants and static property, when class is declared, in order
>>> of their declaration in the class
>>> - instance property, when class is instantiated, in order of their
>>> declaration in the class, before construct
>>> - parameter defaults and attribute arguments defaults, when
>>> function/method/attribute construct is called, in order of the declared
>>> parameter/arguments.
>>>
>>> That sounds good to me.
>>> Thanks!
>>>  Alex
>>>
>>
>> I've updated the RFC (and implementation) to evaluate class constants and
>> static properties at time of class declaration. As such, everything should
>> have a well-defined evaluation order now.
>>
>> However, this also means that this RFC now also contains a
>> backwards-compatibility break: Anything used inside class constant / static
>> property initializers needs to actually be available at the time the class
>> is declared. You can't first declare the class, then declare some
>> additional constants it uses, and then use it.
>>
>
> Another complication here is preloading. The current semantics of
> evaluation on first use work well there, because class loading (during
> preloading) is decoupled from evaluation (during request). Now, we can't
> evaluate initializers during "opcache_compile_file" style preloading, so
> we'd have to delay this to the start of the request. And then we'd have to
> evaluate initializers for all preloaded classes, regardless of whether they
> will be used in this particular request or not. Also opens up the question
> of the order in which the classes should be evaluated.
>
> I initially liked the idea of evaluating everything at the time of class
> declaration, but now get the impression that this causes more problems than
> it solves, and we should go back to the previous lazy evaluation approach.
> Ultimately, my view here is that side-effecting constructors are a terrible
> idea, and if you use them then you should also carefully manage those
> side-effects yourself.
>

Just to throw it out there, another option would be to not support "new"
for static properties and class constants, where the semantics are less
clear cut than in all the other cases. For me personally the important use
cases here are initializers for non-static properties and parameters, as
well as attribute arguments, and those all have unambiguous semantics. Of
course, there is also a lot of value in having consistent support.

Regards,
Nikita

Reply via email to