title: Right way to instantiate class in PHP
body: <p>I am trying to create a method inside class, that will instantiate class that is currently in. But I would also need that from this method to work correctly in all extended classes. As I have learned from <a href="https://stackoverflow.com/questions/5197300/new-self-vs-new-static">this thread</a>, it's not good to use <code>self</code> keyword for this task. So obvious choice would be using <code>static</code> keyword.</p>
  
  <p>But, I've come across different method that also works.</p>
  
  <p>Example:</p>
  
  <pre><code>class SimpleClass
  {
      private $arg;
  
      public function __construct( $arg ){
          $this-&gt;arg = $arg;
      }
  
      public function getArg(){return $this-&gt;arg;}
      public function setArg($arg){$this-&gt;arg = $arg;}
  
      public function staticInstance()
      {
          return new static( $this-&gt;arg );
      }
  
      public function thisInstance()
      {
          return new $this( $this-&gt;arg );
      }
  
      public function selfInstance()
      {
          return new self( $this-&gt;arg );
      }
  }
  
  class ExtendedClass extends SimpleClass
  {
  }
  
  $c1 = 'SimpleClass';
  $c2 = 'ExtendedClass';
  
  $inst1 = new $c1('simple');
  $inst2 = new $c2('extended');
  
  $static_instance_1 = $inst1-&gt;staticInstance();
  $this_instance_1 = $inst1-&gt;thisInstance();
  $self_instance_1 = $inst1-&gt;selfInstance();
  
  $static_instance_2 = $inst2-&gt;staticInstance();
  $this_instance_2 = $inst2-&gt;thisInstance();
  $self_instance_2 = $inst2-&gt;selfInstance();
  
  echo "SimpleClass Instances\n";
  echo get_class($static_instance_1);
  echo get_class($this_instance_1);
  echo get_class($self_instance_1);
  
  echo "ExtendedClass Instances\n";
  echo get_class($static_instance_2);
  echo get_class($this_instance_2);
  echo get_class($self_instance_2);
  </code></pre>
  
  <p>As I can see from this example, both <code>staticInstance</code> and <code>thisInstance</code> produce "correct" results. Or do they?</p>
  
  <p>Can someone explain difference between these two methods and which one is "correct" one.</p>