title: How to cast a parent class to its child class in PHP?
body: <p>Is it possible to cast a parent class to its child in PHP? If not, what is the most efficient alternative to effecting this?</p>
  
  <p>For example, the following results in an error:</p>
  
  <pre><code>&lt;?php
  class Foo {
      public $test;
      public static function create(): self {
          $a = new self();
          $a-&gt;test = 123;
          return $a;
      }
  }
  
  class Bar extends Foo {
      public $baz;
      public static function create(): self {
          $b = (self) parent::create(); // &lt;--- Is this possible?
          $b-&gt;baz = 456;
          return $b;
      }
  }
  
  $bar = Bar::create();
  
  var_dump($bar); /* should output:
    object(Bar)[35]
      public 'baz' =&gt; int 456
      public 'test' =&gt; int 123 */
  </code></pre>