Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Now that PHP 5.3 is widely available in the wild, I wanted to put together a summary answer to this question to reflect newly available techniques. </p> <p>As mentioned in the other answers, PHP 5.3 has introduced <a href="http://php.benscom.com/manual/en/language.oop5.late-static-bindings.php" rel="nofollow noreferrer">Late Static Binding</a> via a new <a href="http://php.benscom.com/manual/en/language.oop5.static.php" rel="nofollow noreferrer"><code>static</code></a> keyword. As well, a new <a href="http://php.benscom.com/manual/en/function.get-called-class.php" rel="nofollow noreferrer"><code>get_called_class()</code></a> function is also available that can only be used within a class method (instance or static). </p> <p>For the purpose of determining the class as was asked in this question, the <code>get_called_class()</code> function is appropriate:</p> <pre><code>&lt;?php abstract class ParentClass { public static function whoAmI () { return get_called_class(); } } class ChildClass1 extends ParentClass { } class ChildClass2 extends ParentClass { } // Shows 'ChildClass1' print ChildClass1::whoAmI(); print "\n"; // Shows 'ChildClass2' print ChildClass2::whoAmI(); print "\n"; </code></pre> <p>The <a href="http://php.benscom.com/manual/en/function.get-called-class.php" rel="nofollow noreferrer">user contributed notes for <code>get_called_class()</code></a> include a few sample implementations that should work in PHP 5.2 as well by making use of <code>debug_backtrace()</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload