title: Invoke parent constructor from child as though it were in the child's scope
body: <h2>Background</h2>
  
  <p>In a project with a PHP 5.6 runtime, I need to work around some third-party code. This code will not be changed by the vendor, nor will it be removed from the codebase.</p>
  
  <p>Specifically, the third-party code (let's call its namespace <code>Theirs</code>) contains a class (<code>\Theirs\BaseClass</code>) whose constructor instantiates another class (<code>\Theirs\Detector</code>).</p>
  
  <p><em>BaseClassTheirs.php</em>:</p>
  
  <pre><code>&lt;?php namespace Theirs;
  
  class Detector {
      public function __construct() {
          print "Theirs\n";
      }
  }
  
  class BaseClass {
      public function __construct() {
          $detector = new Detector();
      }
  }
  </code></pre>
  
  <p>I do not want <code>BaseClass</code> to instantiate <code>\Theirs\Detector</code>. Instead, I want <code>BaseClass</code> to instantiate a <em>different</em> <code>Detector</code> class, from a <em>different</em> namespace (<code>Mine</code>) that is outside of the third-party's control.</p>
  
  <p>In all other respects, though, I want <code>BaseClass</code> to behave as it does in the third-party code, including if the vendor later adds additional functionality to <code>\Theirs\BaseClass</code>. (I'll call this property "<strong>non-fragility</strong>" and the lack of it "<strong>fragility</strong>".) As such, it seems sensible for me to create my own <code>\Mine\BaseClass</code> as a child of <code>\Theirs\BaseClass</code>, inheriting everything from it.</p>
  
  <p>If I take the <strong>fragile, non-<a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow noreferrer">DRY</a> approach</strong> of copy-pasting <code>\Theirs\BaseClass</code>'s constructor into <code>\Mine\BaseClass</code>, then <strong><code>\Mine\Detector</code> is instantiated, as I desired</strong>:</p>
  
  <p><em>BaseClassMine.php</em>:</p>
  
  <pre><code>&lt;?php namespace Mine;
  
  include "BaseClassTheirs.php";
  
  class Detector {
      public function __construct() {
          print "Mine\n";
      }
  }
  
  class BaseClass extends \Theirs\BaseClass {
      public function __construct() {
          $detector = new Detector();
      }
  }
  
  \\ Prints "Mine"
  $obj = new BaseClass();
  </code></pre>
  
  <p>However, if I change this into a <strong>DRY, non-fragile approach</strong> by removing the duplicated code so that <code>\Mine\BaseClass</code> invokes exactly the same constructor, but as inherited from its parent rather than being copy-pasted, then <strong><code>\Theirs\Detector</code> gets invoked, which is not what I want</strong>:</p>
  
  <p><em>BaseClassMine.php</em>:</p>
  
  <pre><code>&lt;?php namespace Mine;
  
  include "BaseClassTheirs.php";
  
  class Detector {
      public function __construct() {
          print "Mine\n";
      }
  }
  
  use \Mine\Detector;
  
  class BaseClass extends \Theirs\BaseClass {
  }
  
  \\ Prints "Theirs"
  $obj = new BaseClass();
  </code></pre>
  
  <p>This happens regardless of whether the file contains a <code>use \Mine\Detector;</code> line, as above.</p>
  
  <h2>Question</h2>
  
  <p>How can I get the best of both approaches?</p>
  
  <p>I.e. how can I invoke <code>\Theirs\Baseclass</code>'s constructor from <code>\Mine\Baseclass</code>'s constructor in order to have it invoke <code>\Mine\Detector</code>, as though <code>\Theirs\Baseclass</code>'s constructor had simply been copy-pasted into <code>\Mine\Baseclass</code>'s context, but without actually copy-pasting it and introducing the corresponding fragility?</p>
  
  <p>For instance, is there a good way to use reflection or some other introspective technique to dynamically read the parent's constructor and to "paste" it at runtime into the child class?</p>
  
  <h2>Related but not identical questions</h2>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/14273057/late-static-binding-without-modifying-parent-class-with-static-keyword">late static binding | without modifying parent class with <code>static</code> keyword</a></li>
  <li><a href="https://stackoverflow.com/questions/23428656/is-there-any-way-to-set-a-property-before-calling-a-constructor">Is there any way to set a property before calling a constructor?</a></li>
  </ul>