-2

Accept my apologies for being beginner for OOP, according to the below code :

Class Test{

   private $name = "youhana";

   function setPrivatePropFromInside(){
      $this->name = "mina";
   }

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

}

Class Test2 extends Test {

}


$obj2 = new Test2();

$obj2->getPrivate();

My Question is that Is Inheritance means to copy from parent to child or the child gain access to the parent visible members?

let me describe why I have confusion by mentioning my thoughts with both of the question members :

  1. In case its a copy, so the invoking the method getPrivate from the child object must return null because the private members will not be copied from the parent to the child, and also the CLASS constant must return Test2 not TEST in case the Inheritance means copying.

so the code mentioned above would equals :

Class Test{

private $name = "youhana";

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

}

Class Test2 extends Test {
   function getPrivate(){
        var_dump(__CLASS__); 
        echo   $this->name ;
    }
}
  1. In case Inheritance means to gain access to the parent visible members, that doesn't refer or point to the concept of inheritance (the children get the characteristics of the parent) despite this concept is logically correct with the results upon executing the mentioned code snippet.

I Read more than 20 References and I still have the confusion, and again accept my apologies for being beginner searching for the correct approach.

Note that: I asked a question Here but after studying more references, I have been returned back to the confusion again, so I need a solid answer.

youhana
  • 318
  • 2
  • 16
  • the main question there was is private members are instantiated or not, but here i have the concept of the inheritance and I faced a question without an answer , so i mentioned it and I mentioned both of points that cause the confusion. – youhana Jan 04 '18 at 13:28
  • My confusion here would be more clear to experts to answer. – youhana Jan 04 '18 at 13:29

3 Answers3

2

Is Inheritance means copy from parent to child or the child gain access to the parent visible members?

The child gain access to the visible members of parent class. Also, you get flexibility to override the feature (method) of parent class.

Ravi
  • 30,829
  • 42
  • 119
  • 173
2

It's nothing to do with "copying".... a class defines methods and properties together with a visibility indicating from where those methods and properties can be accessed.

When one class extends another, then you are creating an inheritence hierarchy or tree, or a set of class contexts.

When you instantiate a child class, you are actually instantiating an object that inherits that full tree.

When you call a method against that instance, it looks to see if that method exists (and is accessible) in the child class definition. If so, it executes it in that context: if not, it looks to see if the method exists in the parent class; and if so, will execute it in that context.

When a method executes in a specific context within the hierarchy of class extensions, it has access to everything visible in that context. Private properties are only accessible in the context where they are defined/the class where they are defined; but methods in that context/class have access to those private properties.

If (as in this case) your public getters and setters exist in the same class/context as the private property, then those methods have access to the private property (because they are in the same class/context), and can be used to access it from outside of that context because the getters/setters themselves are public.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
-1

I think your confusion comes from thinking that

private $name = "youhana";

is a static value which exists in class declaration. In any case, that is just a shorthand for declaring variable values in constructor so even if inheritance "copy" from parent its a wrong example to show that. What you should be asking is: do static properties get copied from the parent? Static properties exists in class declaration and any modifications are visible to all objects of that class. php docs on static properties
Answer is no. Only reference to parent's value is passed. I wrote a simple example to illustrate

<?php
class Foo
{
    public static $my_static = 'foo';

    public function staticValue() {

        return self::$my_static;
    }

    public function modifyFooStatic(){
        return self::$my_static .= '+';
    }
}

class Bar extends Foo
{
    public function fooStatic() {
        return self::$my_static;
    }
    public function modifyBarStatic(){
        return self::$my_static .= '-';
    }
}

$foo = new Foo();
$foo->modifyFooStatic();
print $foo->staticValue() . "\n"; // foo+


$bar = new Bar();
$bar->modifyBarStatic();
print $bar->fooStatic() . "\n"; // foo+-
?>

As you can see both child and parent modifies the same variable even if child calls it with sellf::$my_static

matval
  • 399
  • 5
  • 11