Re: Protected inheritance hierarchies
| From: | Rob Landers | Date: | Sat, 02 Aug 2025 09:07:21 +0000 |
| Subject: | Re: Protected inheritance hierarchies | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-128367@lists.php.net to get a copy of this message | ||
On Tue, Jul 29, 2025, at 20:11, Jonathan Vollebregt wrote:
> I came across this edge case today:
>
> https://3v4l.org/R3Q8D
>
> Both psalm and phpstan think this code is A-OK (Once you add the
> requisite type hints) but it causes fatal errors way back to PHP 5.0.0
>
> I believe classes should be able to access protected properties on their
> siblings if the property in question was declared by a shared parent,
> but it seems both the engine and reflection (ie. getDeclaringClass)
> think that redeclaring a protected property makes it a property of the
> child, not the parent.
>
> This is particularly confusing since the parent class *can* access the
> child class' redeclared protected property, only the sibling can't.
>
> Properties were invariant until the introduction of property hooks, so
> the only edge cases I can think of would be in property hooks (But when
> the input is correctly typed this shouldn't be a problem either)
>
> Is there a technical reason for this behavior or would it be possible to
> relax this?
>
> - Jonathan
>
It's not an edge case, in C2, you redefine a protected variable with the same name and shadowed
the original $v. That $v is different than C's $v. It's easiest to see this with static
access: https://3v4l.org/0SRWb#v8.4.10
However, I don't know of any way to unshadow a property from $this to access the
ancestor's value (other than using private access), but it exists and takes up memory; just
accessing it is the hard part.
— Rob