Emulate get_called_class for PHP < 5.3

This snippet of code will emulate get_called_class function for PHP < 5.3. This will come handy if you want to use Singleton pattern using base abstract class.


if ( !function_exists('get_called_class') ) {
  function get_called_class() {
    $bt = debug_backtrace();
    $lines = file( $bt[1]['file'] );
    preg_match(
      '/([a-zA-Z0-9\_]+)::' . $bt[1]['function'] . '/',
      $lines[ $bt[1]['line'] - 1 ],
      $matches
    );
    return $matches[1];
  }
}

Reference : http://stackoverflow.com/questions/7902586/extend-a-singleton-with-php-5-2-x