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
{
     protected $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


This is correct behavior. This is the same behavior that most object-oriented languages have, including Java and C#.

--
Stephen Coakley

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to