> Le 1 juil. 2024 à 19:02, Larry Garfield <[email protected]> a écrit :
>
> Hi folks. As Ilija's been polishing off hooks to get the PR merged, we've run into
> two small revisions that should make life better for all involved. One is a performance improvement
> that requires a very slight error handling behavior change, and the other is enabling readonly in
> selected (but probably all of the relevant) circumstances.
>
> I'd say we expect these to be uncontroversial, but this is PHP. :-) So I will instead
> just note that it's a short RFC and open the discussion accordingly.
>
> https://wiki.php.net/rfc/hook_improvements
>
> --
> Larry Garfield
> [email protected]
Hi,
1. Removing the guard against recursion is sensible to me, as the sort of bug it is supposed to
prevent is not specific to property accessors. IMO, a better solution to the issue is to implement a
global upper limit on the call stack size. Currently, I am able to generate a call stack of more
than 10 millions items before an OOM error occurs; PHP should have thrown a stack overflow error
long before that. But this is entirely orthogonal to property hooks.
2. As for readonly, I think that the invariant it is supposed to provide should be enforced as
strictly as possible. It means that readonly
is only acceptable if there is no
get
hook.
The primary reason I would want readonly with get
hook, is lazy initialisation, and I
think that there is better design than to trust the user for not implementing bugs. For that use
case, I think it is preferable to implement dedicated hook for lazy initialisation, orthogonal to
the readonly feature. Here is what I would suggest (in a separate RFC, of course):
* An additional hook, called init
, may be added to backed properties. When attempting
to read the value of the backing store, if it is uninitialised, then the init
hook is
triggered, which is supposed to initialise it.
In general, a hooked property can be marked as readonly
when it is backed and has no
get
hook. In particular, it can have an init
hook, which is all we need in
order to have a lazy readonly property that is robust against bugs by construction.
—Claude