Re: [RFC[ Property accessor hooks, take 2
| From: | Robert Landers | Date: | Thu, 21 Mar 2024 16:47:09 +0000 |
| Subject: | Re: [RFC[ Property accessor hooks, take 2 | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-122712@lists.php.net to get a copy of this message | ||
On Thu, Mar 21, 2024 at 5:15 PM Larry Garfield <larry@garfieldtech.com> wrote:
>
> On Thu, Mar 21, 2024, at 9:10 AM, Robert Landers wrote:
> > Hello,
> >
> > I'm a bit confused on inheritance. In the following example of a
> > proxy, do I need to be aware of a parent's hook and handle it
> > specially?
> >
> > class Loud
> > {
> > public string $name {
> > get {
> > return strtoupper($this->name);
> > }
> > }
> > }
> >
> > class LoudProxy extends Loud
> > {
> > public string $name {
> > get {
> > // detected the parent has a hook? //
> > $return = parent::$name::get();
> > // do something with return //
> > return $return;
> > }
> > }
> > }
> >
> > what happens if the Loud class later removes its hook implementation
> > (ex: moves it to the set hook)? Will my proxy now cause an error?
> >
> > Would simply calling $this->name call the parents hook?
>
> Per the RFC:
>
> "If there is no hook on the parent property, its default get/set behavior will be used.
> "
>
> so parent::$name::get() will "read the parent property", which will go through a hook
> if one is defined, and just read the raw value if not. So there is no detection logic needed, and
> the parent can add/remove a hook without affecting the child.
>
> Calling $this->name in LoudProxy's get hook will access backing property on LoudProxy
> itself, ignoring the parent entirely.
>
> --Larry Garfield
Awesome! Thanks! I just wasn't sure what "default" meant in this context.
Robert Landers
Software Engineer
Utrecht NL