-1

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

  1. 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?

youhana
  • 318
  • 2
  • 16
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/162523/discussion-on-question-by-youhana-is-subclass-inherits-private-members-from-pare). – Andy Jan 04 '18 at 14:16

2 Answers2

2

When you extend from a superclass, nothing gets "cloned". That's the wrong way to look at this. The class is basically a template for creating an instance.

The private attributes in the superclass are visible only to the methods, that have been defined in that superclass. That's why it is called "visibility".

The private attributes are still there, when you create a child class. They are just not visible from the methods, that have been defined in the child class.

class Foo {
    private $x = 1;        
    public function check() {
        return $this->x;
    }
}

class Bar extends Foo {
    public $x = 2;
}

class Buz extends Foo {
    private $x = 3;
    public function check() {
        return $this->x;
    }
}

$n = new Bar;
var_dump($n); /*
 object(Bar)#1 (2) {
   ["x"]=>
   int(2)
   ["x":"Foo":private]=>
   int(1)
 } */
var_dump($n->check()); // int(1)

$k = new Buz;
var_dump($k->check()); // int(3)

https://3v4l.org/KkTD2

When you extend a class, you are are creating a specialized subtype case, which should be still compatible with the original superclass. The point of private, when used with extend, is to defined the parts, that will not be subject to any changes in behavior.

As for __CLASS__, it is a "magic constant" (implying, that is not actually constant-ish), that being determined by the context in which it gets accessed. It will give you the name of class, inside of which it then __CLASS__ was written.

Though, I have no idea, what you were trying to do with it, since you should not it in production code.

tereško
  • 58,060
  • 25
  • 98
  • 150
  • so you mean that inheritance means calling and accessing the parent class only , does this correct ? your perfect example proves that. – youhana Jan 04 '18 at 10:47
  • When you have an object, you are not actually "calling a class". Instead you are interacting with an instance, that was made based on the "template". The `extend` keyword is supposed to be used to add **more specialized** behavior (see: [LSP](https://en.wikipedia.org/wiki/Liskov_substitution_principle)). Also, you English sucks :( – tereško Jan 04 '18 at 10:56
  • @tereško can you please add an explanation for scope resolution of the [magic constants](http://php.net/manual/en/language.constants.predefined.php) `__CLASS__`, to address the OP's second question? – Will B. Jan 04 '18 at 11:37
  • 1
    Inheritance is not subtyping. Subtyping refers to compatibility of interfaces. Inheritance refers to reuse of implementations. Google "inheritance vs subtyping", "subclassing vs subtyping" and see papers by William Cook and Alistair Cockburn for more info. – reaanb Jan 04 '18 at 11:42
  • @reaanb thnx. Cleared it up a bit. It was just meant as an expression, not as a technical term. And, no, "expression" in the previous sentence was not used as a technical term either. – tereško Jan 04 '18 at 11:54
-1

I wanna to explain about Cloning concept.

Assume, we have classA with 3 attributes:

 - private int x
 - protected int y
 - public int z

Then we have a ClassB that inherited from ClassA.

In this case, we use Cloning as Structural Cloning. Means that all attributes and methods of parent class clone into child class. We can prove it with by getting the size of child class instances.

But, their is accessing strategy. This is Encapsulation in OOP.

ClassB has x,y,z attributes. But it do not access to x. (because of encapsulation)

The structural cloning concept is important at runtime. In runtime, there is not any relationship between child and parent. The all structure of parent cloned to child. In the child class, we can access to this cloned part with keywords like super (in java) and base (in C#) and etc.

Finally as I said, In child classes, we have private members of parent class. And we can access them with provided keywords by language. But, we can not access the privates, because of Encapsulation Principle.

  • No. Cloning in PHP is a specific thing: https://secure.php.net/manual/en/language.oop5.cloning.php – tereško Jan 05 '18 at 11:01
  • Disclaimer: I do not know PHP, my profession in Java. However, please consider that I talk about **Structural Cloning** means that copying structure of classes in the compiler process. **Not** Object Cloning in programming. –  Jan 07 '18 at 12:52