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
Hi,
just stumbled upon a strange issue.
I always thought that protected/private member variables can only be
altered from inside the object.
Yeah, as far as I know, PHP has always interpreted private and protected as "available to this class" rather than "available to this instance". I'm not sure why, but it's probably not something that can be changed at this stage.
Regards,
Rowan Collins
[IMSoP]
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
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
Hi!
just stumbled upon a strange issue.
I always thought that protected/private member variables can only be
altered from inside the object.
No, it's not correct. Private members are visible withing this class'
scope, protected members are visible within this class' hierarchy.
The reason is that private is considered a detail of implementation of
particular class (thus visible to this class only), and protected is a
member of this class internal API (i.e. visible to extending classes but
not to external users of the class).
Stas Malyshev
smalyshev@gmail.com