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
27 comments on “Late Static Binding in PHP < 5.3
  1. Safe driver says:

    Our Safe Driver Drivers have excellent driving skills and in-depth knowledge of the best routes, helping you reach your destination promptly. They arrive dressed in business casual attire and work hard to make your journey comfortable and enjoyable.
    https://awesomedrive.ae/

  2. Whether you choose to unwind on your private balcony or take advantage of the hotel’s pool and fitness facilities, this Family Hotel Apartment with Dubai Canal view is sure to make your stay unforgettable.

  3. We pride ourselves on offering a unique and technologically-advanced experience through our innovative in-room hospitality facilities.
    https://royalcontinentalhotels.com/rcs/about-us/

  4. If you’re looking for a charming and elegant flower shop in Dubai, look no further than Blush Petals Flower Shop located in Dubai Marina. As soon as you step inside, you’ll be greeted by the sweet scent of fresh blooms and the sight of colorful arrangements that are sure to captivate your senses. The shop is adorned with delicate vases filled with roses, lilies, orchids, and many more exquisite flowers sourced from local growers. The talented florists at Blush Petals have an eye for detail and creativity, crafting stunning bouquets for all occasions – whether it’s a romantic gesture or a special celebration. With their impeccable service and commitment to quality, this flower shop in Dubai is truly a hidden gem waiting to be discovered by flower enthusiasts alike.

  5. Looking for online flower delivery in Dubai, Abu Dhabi, Ajman, Fujairah, Ras al-Khaimah, Sharjah & Umm al-Quwain? We delivers fresh flowers all over UAE.

  6. xlpe says:

    It is easy to work in fabricating and thermoforming. 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/airmaster-xlpe-insulation/

  7. xlpe says:

    XLPE is chemically crosslinked Polyolefin foam is extremely fine closed cells foam and smooth skins on both sides. It is light in weight, flexible, soft in touch, strong and tough in physical properties, resilient and resistant to moisture, chemicals and extreme temperature. It typically offers superior thermal stability and insulating properties improved dimensional consistency and stability over a wide range of fabrication methods and end-user’s conditions. It is easy to work in fabricating and thermoforming. 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/airmaster-xlpe-insulation/

  8. The Royal Continental Hotel is a 4 star luxury business hotel located in the heart of Dubai. We offer our guests the best in business and leisure amenities, with state-of-the-art facilities and world-class services.
    The Royal Continental Hotel is the perfect choice for business and leisure travelers alike.
    https://royalcontinentalhotels.com/about-royal-continental-hotel-dubai/

  9. 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/about-royal-continental-hotel-dubai/

  10. Royal Continental Suites 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. We pride ourselves on offering a unique and technologically-advanced experience through our innovative in-room hospitality facilities.

    https://royalcontinentalhotels.com/rcs/

  11. Looking for Dermal Fillers Dubai? American Accelerated Aesthetics Plastic Surgery best specialists for dermal fillers in Dubai. Book now !
    https://aaap.ae/our-services/derma-fillers-dubai/

  12. Buy flowers online Dubai from blush and petals flower shop in Dubai Marina. We offer free shipping* and same day delivery in Dubai.
    https://blushpetals.ae/collections/buy-flowers-online-dubai

  13. Safe driver says:

    If you have a car and are too lazy to drive around, we are here to help you. Our professional drivers will take you to any location within The United Arab Emirates.

  14. Royal Continental Hotel(hotel close to Dubai International Airport) 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.

  15. Royal Continental Hotel(hotel close to Dubai International Airport) offers business travelers and holidaymakers a stylish and welcoming haven just minutes from Dubai International Airport.
    https://royalcontinentalhotels.com/hotels-close-to-dubai-international-airport/

  16. Foremost Driver is one of the leading chauffeured car service providers in the UAE. When you are looking for a reliable Safe Driver Dubai for yourself or your loved ones, our best drivers will meet your needs. Hire our skilled drivers on an hourly, daily, weekly, or monthly basis. Whether you need a one-time service or a Monthly Driver in Dubai, we’ve got you covered. Our services are customized to our customers’ needs. We have expertise in providing you with the best safe driving services. Just give us a call and let Foremost Driver handle the rest.

    We provide 24/7 professional chauffeured car services to fulfill your traveling needs within Dubai and the UAE.

    Contact Us:
    +971 52 335 5475
    https://foremostdrive.ae/services/monthly-driver-in-dubai/

  17. At Seha Garden International Hospital, our Stroke Rehabilitation program helps survivors relearn skills that are lost following a stroke. Through a goal-oriented treatment plan that involves physical medicine, psychiatry, Ayurveda, physiotherapy, occupational and speech therapies, our cross-functional team works together to teach stroke patients new ways of performing tasks so they can regain independence and improve their quality of life.

    https://sehagarden.com/treatments/stroke-rehabilitation/

  18. 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/

  19. 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/

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

  21. 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/

  22. 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/

  23. 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/

  24. Tobias says:

    Thanks, you saved my day :)

  25. Marcelo says:

    Thanks ;)

Leave a Reply to hotel apartments in dubai Cancel 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>