Late Static Binding in PHP < 5.3

Use case

Even though PHP 5.3 is already out since more than one and a half year, a lot of web-hosters still don’t provide this version. In one of my projects I had to deal with the webhoster all-inkl. The project is written in PHP 5.3, but all-inkl supports PHP 5.2 only. Indeed they offer the possibility to use CGI version for PHP >5.2, but that doesn’t make me happy. So I tried to downgrade the project as successfull as possible.. But when it comes to 5.3′s Late Static Binding I faced problems to rebuild this functionality within 5.2. But after hours of searching the web and testing each approaches, I wrote my own hunky-dory function to fake LSB.


if( !function_exists( 'get_called_class' ) ) {
  class class_tools {        

    static $i = 0;        
    static $fl = null;

    static function get_called_class() {            
      $bt = debug_backtrace();
      if( self::$fl == $bt[2]['file'].$bt[2]['line'] ) {
      } else {                
        self::$i = 0;                
        self::$fl = $bt[2]['file'].$bt[2]['line'];            

      $lines = file($bt[2]['file']);
      return $matches[1][0];        

  function get_called_class() {        
    return class_tools::get_called_class();    


This faked LSB gets only┬áincluded if no native support for get_called_class exists (PHP <5.3). Otherwise the parser won’t recognize this code. Even though a lot of code examples don’t deal with the second index of the debug_backtrace-result ($bt[2]), for me it was/is the only way to make that LSB work correctly.

Tagged with: , , , ,
Posted in PHP
4 comments on “Late Static Binding in PHP < 5.3
  1. Tobias says:

    Thanks, you saved my day :)

  2. Marcelo says:

    Thanks ;)

Leave a Reply

Your email address will not be published. Required fields are marked *


Anti-spam protection

Prove that you are Human by typing the emphasized characters:

Protected by Gab Captcha 2

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>