Skip to content

Instantly share code, notes, and snippets.

@LiamChapman
Created February 21, 2014 09:56
Show Gist options
  • Select an option

  • Save LiamChapman/9131635 to your computer and use it in GitHub Desktop.

Select an option

Save LiamChapman/9131635 to your computer and use it in GitHub Desktop.
Add get_called_class support for php < 5.3. It will be slower though as it uses debug_backtrace.
<?php
if (!function_exists('get_called_class') && version_compare(PHP_VERSION, '5.3', '<')) {
function get_called_class() {
$bt = debug_backtrace();
$l = 0;
do {
$l++;
$lines = file($bt[$l]['file']);
$callerLine = $lines[$bt[$l]['line']-1];
preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l]['function'].'/', $callerLine, $matches);
} while ($matches[1] === 'parent' && $matches[1]);
return $matches[1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment