Originally Posted by clam2000
so why does overloading the getprop method cause php to update the private property?
It doesn't.
"Private limits visibility only to the class that defines the item."
If you want your child classes to have direct access to the private properties/methods, you need to use protected, not private.
In the b and c class constructors, when you are doing $this->prop = 1, you are actually creating a new public property, "prop". You can verify this with print_r or var_dump:
Code:
php > $c = new c();
php > print_r($c);
c Object
(
[prop: private] => -1
[prop] => 1
)
php >
So, c::getprop is actually returning the public prop of object c.
The same is true of b as well. It has the private prop (which it cannot see), and a public prop from the constructor. But b::getprop is actually a::getprop. a has access to the private prop, so it returns that and not the public prop of b.
It has to do with visibly. You aren't changing the private property of a, but are creating a new public property.
We can go one further to help demonstrate this:
[php]class d extends a
{
function getprop() {
return $this->prop;
}
}[/php]
Code:
php > $d = new d();
php > echo $d->getprop();
PHP Notice: Undefined property: d::$prop in php shell code on line 4
Notice: Undefined property: d::$prop in php shell code on line 4
php >
Here we get a warning, because d cannot see the private prop, and we've not set a public one either.
It does seem a bit odd, but I don't think it is a bug. Normally you cannot have properties of the same name in a class even with different visualities. However, in b & c, they cannot see the property "prop" since it is private to a, so you can redeclare it, which is basically what is going on with your test code.
I tend to stay away from private as there is really no example I can think of where you would want to shield properties or methods from child classes, so mostly I stick to protected and public.