PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
class abc{
protected $z = 45;
public function demo(){
print $this->z;
}
}
class abc1 extends abc{
public function demo(){
print $this->z; // works well it prints 45 as expected
}
}
class abc2 extends abc1{
public function demo2(){
print "---".$this->z; // it also prints 45 but its unexpected , because protected member variables and methods
are accessible to only first level child class according to OOP.
}
}
$obj = new abc2();
$obj->demo2();
I am using PHP 5.3.0
Any suggestions would be greatly appreciated.
Thanks
Last edited by Weirdan on Wed Dec 01, 2010 7:44 am, edited 1 time in total.
Reason:added syntax highlighting
// it also prints 45 but its unexpected , because protected member variables and methods
are accessible to only first level child class according to OOP.
Citation needed!
protected remains protected in the child, it does not change to private, unless you change it (which PHP won't allow, you can only change protected to public)
And actually there is no reason to limit the visibility only to first child, actually in OOP the parent class should not know anything about the children, even that there are children (although you can limit class inheritance declaring class / method / etc as final)
Thanks for your reply, I know until we do not change the visibility of a protected variable of parent class
in child class by re declaring it it remains protected.
According to you if protected members of parent class are also accessible to the second level of child class
then there is no meaning of protected members.
mudgil.gaurav wrote:According to you if protected members of parent class are also accessible to the second level of child class
then there is no meaning of protected members.
I, for one, don't like it when people put lies into my mouth. Before you go about saying things like "there is no meaning to this", consider learning what you're talking about first: there's a big difference between sounding educated and being educated. Don't forget that you're the one asking the questions.
Thanks for your reply, I know until we do not change the visibility of a protected variable of parent class
in child class by re declaring it it remains protected.
According to you if protected members of parent class are also accessible to the second level of child class
then there is no meaning of protected members.
We can either make them public or private.
Thanks
Gaurav
Do you know the difference between private and protected? Private is not available for child classes, protected is. So what is the point to make something available for first level child, but not for second level? Isn't it the same as removing a public member at the second level of inheritance?
Yes i understand the difference between private and protected.My concern was only if class Y extends Class X then
protected member variables of class X is accessible in Class Y but if Class Z extends class Y then class Z
can also access the class X protected properties.
But i do not know which way you translated my post , and if you felt anything uncomfortable then i am really very sorry.
mudgil.gaurav wrote:My concern was only if class Y extends Class X then
protected member variables of class X is accessible in Class Y but if Class Z extends class Y then class Z
can also access the class X protected properties.sorry.
Thanks
Gaurav
This is perfectly correct.
If you have brown eyes, your child will have brown eyes too, and children of your child will have brown eyes too...
This is inheritance. All protected members are available for the whole class tree, much like all of the public members. The only difference is that protected members are not accessible by members, which are not part of the class family