Hi guys,
I am newbie in PHP5 and I am confused about the access of protected items.According to the tutorial "Protected limits access to inherited classes (and to the class that defines the item).", but it is possible too to access a protected item from the parent.Can anyone explain me how it is possible?
Thanks in advance.
Hier is an example of working code:
class A {
public function publicFunction(A $obj) {
echo $obj->attribut;
echo '<br />';
$obj->protectedFunction();
}
}
class B extends A {
protected $attribut = 'I am protected attribut';
protected function protectedFunction() {
echo 'In protected function';
}
public function testPublic() {
return parent :: publicFunction($this);
}
}
$instanceB = new B;
$instanceB->testPublic();
exit;change $obj-> to $this->In your example you are passing an instance $obj to the parent. This is not the same as accessing a child variable from within the parent. However the parent does indeed have access to protected members in it's children.
class Test
{
public function tester()
{
echo $this->test;
}
}
class Test2 extends Test
{
protected $test = 'child';
}
$t = new Test2;
$t->tester(); // outputs 'child'
When you extend a class the resulting object is considered to be an instance of both classes so it might make sense that the parent has visibility of child variables. Private members and methods can't be accessed this way, and additionally if the parent defines a variable as private, and the child defines the same named variable as protected, they remain separate as you would expect. I wonder how Java works in this respect.
You are right that the documentation is not complete here In your example you are passing an instance $obj to the parent. This is not the same as accessing a child variable from within the parent. However the parent does indeed have access to protected members in it's children.
class Test
{
public function tester()
{
echo $this->test;
}
}
class Test2 extends Test
{
protected $test = 'child';
}
$t = new Test2;
$t->tester(); // outputs 'child'
When you extend a class the resulting object is considered to be an instance of both classes so it might make sense that the parent has visibility of child variables. Private members and methods can't be accessed this way, and additionally if the parent defines a variable as private, and the child defines the same named variable as protected, they remain separate as you would expect. I wonder how Java works in this respect.
You are right that the documentation is not complete here
Hi guys,
10x for the responses, but I am still confused about this.
@Shrike :
In your example you use a property from the child class in the parent class.Yes I agree that this works, but in my opinion it doesn't make sense.
Inheritance mean that one class extended another and add a new functionality.
How can the parent class know, how will it be extended in the future.The parent class can not know the properties and the methods of the child classes unless the methods are defined as abstract ... in this case the parent know that this methods must be implemented and must exist.But if the methods are not defined as abstract methods in the parent, the parent can not use them without reference to the child class (as in my example)
I am newbie in PHP5 and I am confused about the access of protected items.According to the tutorial "Protected limits access to inherited classes (and to the class that defines the item).", but it is possible too to access a protected item from the parent.Can anyone explain me how it is possible?
Thanks in advance.
Hier is an example of working code:
class A {
public function publicFunction(A $obj) {
echo $obj->attribut;
echo '<br />';
$obj->protectedFunction();
}
}
class B extends A {
protected $attribut = 'I am protected attribut';
protected function protectedFunction() {
echo 'In protected function';
}
public function testPublic() {
return parent :: publicFunction($this);
}
}
$instanceB = new B;
$instanceB->testPublic();
exit;change $obj-> to $this->In your example you are passing an instance $obj to the parent. This is not the same as accessing a child variable from within the parent. However the parent does indeed have access to protected members in it's children.
class Test
{
public function tester()
{
echo $this->test;
}
}
class Test2 extends Test
{
protected $test = 'child';
}
$t = new Test2;
$t->tester(); // outputs 'child'
When you extend a class the resulting object is considered to be an instance of both classes so it might make sense that the parent has visibility of child variables. Private members and methods can't be accessed this way, and additionally if the parent defines a variable as private, and the child defines the same named variable as protected, they remain separate as you would expect. I wonder how Java works in this respect.
You are right that the documentation is not complete here In your example you are passing an instance $obj to the parent. This is not the same as accessing a child variable from within the parent. However the parent does indeed have access to protected members in it's children.
class Test
{
public function tester()
{
echo $this->test;
}
}
class Test2 extends Test
{
protected $test = 'child';
}
$t = new Test2;
$t->tester(); // outputs 'child'
When you extend a class the resulting object is considered to be an instance of both classes so it might make sense that the parent has visibility of child variables. Private members and methods can't be accessed this way, and additionally if the parent defines a variable as private, and the child defines the same named variable as protected, they remain separate as you would expect. I wonder how Java works in this respect.
You are right that the documentation is not complete here
Hi guys,
10x for the responses, but I am still confused about this.
@Shrike :
In your example you use a property from the child class in the parent class.Yes I agree that this works, but in my opinion it doesn't make sense.
Inheritance mean that one class extended another and add a new functionality.
How can the parent class know, how will it be extended in the future.The parent class can not know the properties and the methods of the child classes unless the methods are defined as abstract ... in this case the parent know that this methods must be implemented and must exist.But if the methods are not defined as abstract methods in the parent, the parent can not use them without reference to the child class (as in my example)