Re: Access of protected members
| From: | Stephen Coakley | Date: | Mon, 03 Aug 2015 16:17:37 +0000 |
| Subject: | Re: Access of protected members | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-87581@lists.php.net to get a copy of this message | ||
On 08/03/2015 04:39 AM, Nicolai Scheer wrote:
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: class Test {This is correct behavior. This is the same behavior that most object-oriented languages have, including Java and C#. -- Stephen Coakleyprotected $member = null;public static function getObj() { $myself = new Test(); $myself->member = "hello world"; return $myself; }} $new_object = Test::getObj(); var_dump( $new_object ); The output is: object(Test)#1 (1) { ["member":protected]=> string(11) "hello world" } 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. Greetings Nico