From: Nicolai Scheer [mailto:nicolai.sch...@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