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.

Solution

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'] ) {
        self::$i++;            
      } else {                
        self::$i = 0;                
        self::$fl = $bt[2]['file'].$bt[2]['line'];            
      }

      $lines = file($bt[2]['file']);
      preg_match_all(
        '/([a-zA-Z0-9\_]+)::'.$bt[2]['function'].'/',                
        $lines[$bt[2]['line']-1],                
        $matches            
      );             
      return $matches[1][0];        
    }    
  }

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

Conclusion

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
10 comments on “Late Static Binding in PHP < 5.3
  1. Awesome Drive is the ultimate choice for exceptional driving service in Dubai. With a commitment to safety, our professional safe driver Dubai ensure a secure and comfortable journey for passengers. Equipped with extensive knowledge of Dubai’s roads and regulations, our drivers guarantee a smooth and efficient ride. Our fleet consists of well-maintained vehicles, and our drivers adhere to strict hygiene protocols.

    https://awesomedrive.ae/

  2. XLPE foams work well in the building and construction industry where thermal insulation, sound and vibration are critical, due to its low moisture permeability and high buoyancy.

    https://airmasteremirates.com/product/cross-linked-polyolefin-foam-xlpe/

  3. Located near Dubai International Airport, the Royal Continental Hotel is one of the Best 4 star Hotels in Dubai that offers excellent service, good value, and good quality.

  4. Are you for in a situation where too exhausted to drive back to home like returning home from a party, long work schedule? Safe driver Dubai is ready to help you…

    https://safedriverdubai.ae/

  5. Royal Continental Hotel – A 4 Star Hotel in Dubai offers business travelers and holidaymakers a stylish and welcoming haven just minutes from Dubai International Airport. With a variety of rooms to suit all requirements and tastes, as well as a host of business and leisure facilities, dining outlets and amenities, Royal Continental Hotel provides the discerning traveler with impeccable accommodation and service in relaxed contemporary surroundings, perfectly suited to both a short city break or an extended vacation
    https://royalcontinentalhotels.com/4-star-hotel-in-dubai/

  6. If you are looking for a Luxury 4-star hotel near Dubai International Airport, near to Deira City Centre, near to Dubai Metro, Royal Continental Hotel is the best option for you. Royal Continental Hotel Dubai is the Best Luxury Business Hotel in Dubai which offers the Best Affordable stay in Dubai, UAE.

    https://royalcontinentalhotels.com/4-star-hotel-in-dubai/

  7. Tobias says:

    Thanks, you saved my day :)

  8. 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>