Proected Member Variables & Methods

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!

Moderator: General Moderators

Post Reply
mudgil.gaurav
Forum Newbie
Posts: 17
Joined: Wed Oct 08, 2008 4:39 am

Proected Member Variables & Methods

Post by mudgil.gaurav »

Do protected member variables and methods are accessible to the second level of inheritance.

Code: Select all

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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Proected Member Variables & Methods

Post by Darhazer »

// 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)
mudgil.gaurav
Forum Newbie
Posts: 17
Joined: Wed Oct 08, 2008 4:39 am

Re: Proected Member Variables & Methods

Post by mudgil.gaurav »

Hi

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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Proected Member Variables & Methods

Post by requinix »

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.
mudgil.gaurav
Forum Newbie
Posts: 17
Joined: Wed Oct 08, 2008 4:39 am

Re: Proected Member Variables & Methods

Post by mudgil.gaurav »

Yes i know i am asking the question , i was no mean to hurt or argument with someone. I wrote what i know
so would love if someone helps.

Thanks
Gaurav
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Proected Member Variables & Methods

Post by Darhazer »

mudgil.gaurav wrote:Hi

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?
mudgil.gaurav
Forum Newbie
Posts: 17
Joined: Wed Oct 08, 2008 4:39 am

Re: Proected Member Variables & Methods

Post by mudgil.gaurav »

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.

Thanks
Gaurav
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Proected Member Variables & Methods

Post by Darhazer »

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
Post Reply