Saturday 20 January 2018

php - Is subclass inherits private members from parent class?

I have two questions related to OOP Inheritance object :




  1. The first one is
    that according to the below
    code:



    class Test
    {


    private $name = "Youhana";


    function getPrivate(){
    echo $this->name ;

    }

    }

    Class Test2 extends Test
    {


    }
    $obj2 = new
    Test2();
    $obj2->getPrivate();



The
result will be => Youhana, but how does this work, despite the inheritance means that
members are cloned from the parent to child, so if this is correct, the code in the
child class must be like the below
logic:



 Class Test2 {// simple
imagination to the Test2 after extends Test CLass

function
getPrivate(){ //which must return null not the value of the private member.

echo $this->name ;

}
}


Ref :
Manual



start="2">
  • Second question is that consider the below
    code:




     Class
    Ex1{

    function checkClassName(){
    var_dump(__CLASS__);

    }
    }

    Class Ex2 extends
    Ex1{


    }


    $obj2 = new
    Ex2();

    $obj2->checkClassName();//how this will return EX1
    although we invoked this function from the second object which after the inheritance
    will have a clone of public and protected members of the parent
    class?



  • How
    this will return EX1 although we invoked this function from the second object which
    after the inheritance will have a clone of public and protected members of the parent
    class?

    No comments:

    Post a Comment

    php - file_get_contents shows unexpected output while reading a file

    I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...