RE: [PHP-DEV] Access of protected members
| From: | Christian Stoller | Date: | Mon, 03 Aug 2015 12:02:38 +0000 |
| Subject: | RE: [PHP-DEV] Access of protected members | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-87573@lists.php.net to get a copy of this message | ||
From: Nicolai Scheer [mailto:nicolai.scheer@gmail.com], Sent: Monday, August 03, 2015 11:39 AM
>
> Hi,
>
> just stumbled upon a strange issue.
> I always thought that protected/private member variables can only be
> altered from inside the object.
>
> This example shows, that this is not true:
> [...]
> Of course, I'm "inside" the right class, nevertheless, the object
> stored in $myself should not allow direct access to its members.
>
> Is this the expected behaviour? Code of this kind is used quite
> frequently for factory methods.
>
This is intended behavior. It is even possible to access private
methods and properties of another instance from the same type.
See example #3 on http://php.net/manual/en/language.oop5.visibility.php
The reason why this is possible is given, too:
> Objects of the same type will have access to each others
> private and protected members even though they are not the
> same instances. This is because the implementation specific
> details are already known when inside those objects.
Best reagrds
Christian