On Thu, Mar 21, 2024 at 5:15 PM Larry Garfield <la...@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

Reply via email to