title: PHP: Why do Inherited Methods of Child Class access Parent's Private Properties?
body: <p>What a curious twist in PHP's property and method inheritance. See the following example:</p>
  
  <pre><code>class A {
      private $foo = 'hello';
  
      function get() {
          return $this-&gt;foo;
      }
      function set($val) {
          $this-&gt;foo = $val;
      }
  }
  
  class B extends A {
      private $foo = 'world';
  
      function get() { // try inheriting this
          return $this-&gt;foo;
      }
      function set($val) {  // try inheriting this
          $this-&gt;foo = $val;
      }
  }
  </code></pre>
  
  <p>Now let's see how that echoes out if we inherit or define methods:</p>
  
  <pre><code>$b = new B();
  
  /* If B defines getter: */
  echo $b-&gt;get(); // 'world' 
  
  /* If B inherits getter: */
  echo $b-&gt;get(); // 'hello'
  
  // Enter Setter!
  $b-&gt;set('cosmos');
  
  /* If B defines setter &amp; inherits getter: */
  echo $b-&gt;get(); // 'hello' 
  
  /* If B inherits setter &amp; defines getter: */
  echo $b-&gt;get(); // 'world' 
  
  /* If B defines or inherits both: */
  echo $b-&gt;get(); // 'cosmos'
  </code></pre>
  
  <p>So: When class B defines the getter, it returns its own private <code>$foo</code> ( <code>= "world"</code>). However if I remove the getter from class B, and class B inherits the very same method from class A, it returns instead the private <code>$foo</code> property from the parent class A (<code>= "hello"</code>) instead. Likewise with setters.</p>
  
  <p>I was expecting inherited methods to access the properties of the instantiated class at all times, with no reference to their ancestors' private properties. They are private for a reason, no? Private with side-effects! If I change the prop visibility to public or protected, the expected properties "world" and "cosmos" from class B are returned, whether methods are defined or inherited.</p>
  
  <hr>
  
  <p>Here are object dumps for each of the above cases:</p>
  
  <pre><code>var_dump($b);
  
  /* If B defines getter (Prop get from B): */
  
  ["foo":"B":private] · string(5) "world"
  ["foo":"A":private] · string(5) "hello"
  
  /* If B inherits getter: (Prop get from A) */
  
  ["foo":"B":private] · string(5) "world"
  ["foo":"A":private] · string(5) "hello"
  
  // Enter Setter!
  $b-&gt;set('cosmos');
  
  /* If B defines setter &amp; inherits getter 
   * Prop set to B &amp; get from A */
  
  ["foo":"B":private] · string(6) "cosmos"
  ["foo":"A":private] · string(5) "hello"
  
  /* If B defines getter &amp; inherits setter
   * :: Prop set to A &amp; get from B */
  
  ["foo":"B":private] · string(5) "world"
  ["foo":"A":private] · string(6) "cosmos"
  
  /* If B defines both:
   * :: Prop set to B &amp; get from B */
  
  ["foo":"B":private] · string(6) "cosmos"
  ["foo":"A":private] · string(5) "hello"
  
  /* If B inherits both:
   * :: Prop set to A &amp; get from A: */
  
  ["foo":"B":private] · string(5) "world"
  ["foo":"A":private] · string(6) "cosmos"
  </code></pre>
  
  <p>Can someone explain to me the logic in what's going on? Are inherited property-access methods bound to the private properties of the original method-defining parent class? I'd rather not copy-paste the same method into each child class that has private properties with a matching name.</p>
  
  <p>Excuse me if the illustrations above go on ad nauseam. Had to spell this out for my own sanity huh -- unexpected behavior, unless I'm missing something obvious. (N.B. Having constructors or invoking <code>parent::__construct()</code> in the child class constructor has no impact on this behavior.)</p>
  
  <hr>
  
  <p><strong>Add:</strong> Now one step further. If I declare the getters protected, and create a public method in class B for getting variables. Like so:</p>
  
  <pre><code>class B {...
      public function getpro_direct() {
          return $this-&gt;foo;
      }
      public function getpro_getter() {
          return $this-&gt;get();
      }
  ...}
  
  echo $b-&gt;getpro_direct(); // "world" (from B)
  echo $b-&gt;getpro_getter(); // "hello" (from A)
  </code></pre>
  
  <p>And if I inherit these two methods from A instead, I get <code>"hello"</code> on both counts. But if I define a <code>get()</code> in B, I get "hello" and "world" respectively. Hello world hello, cosmos calling. I need some juice.</p>
  
  <hr>
  
  <p><strong>Edit</strong> for those of you wondering why I'm at this. The short background story: I'm working on a class hierarchy that grows in more depth in (array) properties, with each child class being a less abstract version down the same vector. (Spare you the examples.) I'm working out the smartest way to merge the child's extended properties with each consecutive parent's basis. </p>
  
  <p>Private declaration was so far the only way to prevent the child class from overwriting the parent's properties, as I could have the properties of both simultaneously available via using the parent getter. That however mucks up the method inheritance, as illustrated here. I'd like to get this done as automagically as possible; preferably with a single <code>clone_parent()</code> method that's inherited and run in each child's constructor. I'd rather avoid spelling out separate cloners for each parent-child extension hop. (I've searched and read around a bunch here, yet to find a satisfactory solution.) </p>
  
  <p><strong>Edit2:</strong> Other approaches pending, on my actual concern (as above) that led to this experiment. To keep things simple, I'll just ditch the overlapping declaration in the child's properties and call an <code>extend_props</code> method that adds atop the props inherited from the arent. (Alas I hoped to have each extending class's extras shown pretty up atop, instead of lurking down below inside a method!)</p>