Access of protected members
| From: | Nicolai Scheer | Date: | Mon, 03 Aug 2015 09:39:05 +0000 |
| Subject: | Access of protected members | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-87565@lists.php.net to get a copy of this message | ||
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