Accessing protected item of a child class from the parent
Results 1 to 5 of 5

Thread: Accessing protected item of a child class from the parent

  1. #1
    Junior Member
    Join Date
    Jul 2005
    Posts
    2

    Accessing protected item of a child class from the parent

    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;

  2. #2
    addicted thorpe's Avatar
    Join Date
    Aug 2004
    Location
    sydney
    Posts
    3,457
    change $obj-> to $this->

  3. #3
    Not Yet Involved Shrike's Avatar
    Join Date
    Oct 2003
    Location
    The Eighth, Sursamen
    Posts
    2,276
    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.
    PHP Code:
    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
    The Hundredth Idiot

  4. #4
    Junior Member
    Join Date
    Jul 2005
    Posts
    2
    Quote Originally Posted by Shrike
    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.
    PHP Code:
    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)

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    1

    Arrow An illustrated example as why it does not work [ on php ]

    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 }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •