title: Get class name of child class in its parent within static context?
body: <p>Consider the following parent class <code>Parent.php</code>:</p>
  
  <pre><code>class Parent {
  
      protected static $className;
  
      public static function __static() {
          static::$className = get_called_class();
      }
  
      public static function doSomething1() { ... }
      public static function doSomething2() { ... }
      public static function doSomething3() { ... }
  }
  Parent::__static();
  </code></pre>
  
  <p>and its child class <code>Child.php</code>:</p>
  
  <pre><code>class Child extends Parent { }
  </code></pre>
  
  <p>Each method <code>doSomething</code> 1-3 needs to access <code>$className</code>. If <code>Child</code> calls <code>doSomething1()</code>, the <code>$className</code> must be <code>Child</code>, and <strong>not</strong> <code>Model</code>. I could certainly do</p>
  
  <pre><code>public static function doSomething() { $className = get_called_class(); }
  </code></pre>
  
  <p>in every method but this would result in code repetition. Plus, <code>$className</code> makes most sense to be declared static. The workaround I used is to call a <a href="https://stackoverflow.com/questions/3312806/static-class-initializer-in-php#3312881">static class initializer</a> after class declaration in <code>Parent</code>. Obviously this didn't work either, because the variable <code>$class</code> is still initilized on <code>Parent</code>, not on <code>Child</code>, <em>despite</em> the use of <code>static</code> (i.e. late binding).</p>
  
  <p>Long story short, <strong>how do I get the following</strong>:</p>
  
  <pre><code>Child::doSomething1(); // -&gt; $className must be 'Child'
  Child::doSomething2(); // -&gt; $className must be 'Child'
  Child::doSomething3(); // -&gt; $className must be 'Child'
  </code></pre>
  
  <p>Let me complicate it a little bit: I have <em>multiple</em> child classes, <code>Child1</code>, <code>Child2</code>, <code>Child3</code>, so placing <code>ChildX::__static();</code> at the end of file in each <code>ChildX</code> class would be inefficient.</p>
  
  <p><em>BONUS</em>: if I call <code>doSomething1()</code> in <code>Child1</code>, does that mean that <code>Child2</code> and <code>Child3</code> will share a static property <code>$className</code> with the value <code>Child1</code>? My guess is no, but I'd love to hear your viewpoint.</p>
  
  <p>Any thoughts on this?</p>