Property hooks, and &get-only write behavior

From: Date: Wed, 12 Jun 2024 15:06:58 +0000
Subject: Property hooks, and &get-only write behavior
Groups: php.internals 
Request: Send a blank email to internals+get-123588@lists.php.net to get a copy of this message
Hi everyone While reviewing the property hooks patch, Bob found a discrepancy in the implementation and the specification. There is an unfortunate mistake in one of the examples that suggests specific by-reference behavior that we did not intend to support. Specifically, from https://wiki.php.net/rfc/property-hooks#references (you'll have to scroll down a bit): ```php class C { public string $a { &get { $b = $this->a; return $b; } } } $c = new C(); $c->a = 'beep'; // $c is unchanged. ``` This example implies that the assignment $c->a = 'beep'; will invoke &get to write to the contents of the reference. However, this is not the case. Direct assignments will, in the absence of set, fall back to the builtin write operation for backed properties, or throw for virtual properties. Instead of $c->a = 'beep';, the example _should_ have said: ```php $ref = &$c->a; $ref = 'beep'; ``` Here, $ref = &$c->a; _will_ call &get. What the example intended to show is that the reference returned by &get is detached from $c->a itself. That said, I did give this idea some thought. I think it would not have been unreasonable to automatically call &get and assign to the returned reference when there is a &get hook but _no_ set hook. However, when trying to implement this, I encountered some technical issues that make this currently unfeasible. So, we'd like to move forward with the original semantics: If there is no set hook, then the assignment will trigger a builtin write for backed properties, or an error for virtual properties. If you wish to achieve the behavior described above (using &get to write to the property), you can implement set to perform the write to the reference yourself. I hope that these semantics are acceptable for everyone. Ilija

« previous php.internals (#123588) next »