Re: [RFC] Property hooks, nee accessors

From: Date: Mon, 15 May 2023 02:20:56 +0000
Subject: Re: [RFC] Property hooks, nee accessors
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-120277@lists.php.net to get a copy of this message
On Tue, May 9, 2023 at 4:38 AM Larry Garfield <larry@garfieldtech.com> wrote: > > Ilija Tovilo and I would like to offer another RFC for your consideration.. It's been a > while in coming, and we've evolved the design quite a bit just in the last week so if you saw > an earlier draft of it in the past few months, I would encourage you to read it over again to make > sure we're all on the same page. I'm actually pretty happy with where it ended up, even > if it's not the original design. This approach eliminates several hard-to-implement edge cases > while still providing a lot of functionality in one package. > > https://wiki.php.net/rfc/property-hooks > > -- > Larry Garfield > larry@garfieldtech.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > Hi Larry. Suppose that I just want to cache once and freeze, and I don't want to create additional hidden property. Please make a judgement on the validity of this syntax: ``` public string $prop1 { get => $field ??= $this->heavyTask($this->propx, $prop->y); set => null; } ``` Explanation: * Since the ``get` hook uses `$field``, the engine will create a backing store value with name ``$prop1``, and will be filled at the first call only. The later call will use the already stored value. * Since no new value is assigned to ``$field` in the `set`` hook, the value stored in ``$prop1`` is still the same as before. * Since any return statement will be discharged in ``set`` hook, the assignment chaining is safe to use and the value which is transferred is alway the rightmost value (not the value returned by the ``set`` hook). ``` $varx = $obj->prop1 = "hello"; // $varx value is "hello", and $obj->prop1 value is unchanged ``` But, this statement below is NOT ALWAYS correct for "cache once and freeze": ``` public string $prop1 { set => $field ??= $this->heavyTask($this->propx, $prop->y); } ``` Explanation: * Since default ``get` hook can be called before `set`` hook, backing store value with name ``$prop1`` will still be empty. * Otherwise, I have to duplicate the body of the ``set`` hook into ``get`` hook to prevent an emptiness backing store. Please give a comment. Thanks.

« previous php.internals (#120277) next »