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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s