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;

    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 🙂

      Shrike wrote:

      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.
      @ :
      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)

        7 years later

        Okay, I can see where this is taking you!

        Look at this example:

        class parentClass {

            protected $iniVar;
        private function init($init){
        
        	foreach($iniVar as $key=>$value){
        
                  /*Do something amazing with each iniVar key/value pair*/
        
        	  }
        
        }
        
        function __construct(){
        
           init();
        
        
        }

        }

        class childClass extends parentClass{

        function __construct(){
        
            $iniVar = array('key'=>'value'); ///so on....  
        
        
        }

        }

        This our reasoning!
        " I just have to extend the class, initialize protected variables in the constructor, and let the parent class handle the rest" -and believe me if it was that easy many people would be chaining commands in php as much as they do on javascript!

        However this what we didn't think:

        We are extending the parentClass which means that the parent constructor is executed first, at this point our protected iniVar is not initialized yet.

        You can prove this by setting a static variable in the parentClass and displaying and incrementing its value from the parentClass' constructor every time it's called, then try to call the parentClass' constructor from the childClass' constructor. Try reversing the roles 😉 { have fun }

          Write a Reply...