Skip to content

Instantly share code, notes, and snippets.

@garethflowers
Last active April 19, 2023 21:55
Show Gist options
  • Save garethflowers/9b869b1c807ecadd0393 to your computer and use it in GitHub Desktop.
Save garethflowers/9b869b1c807ecadd0393 to your computer and use it in GitHub Desktop.
Polyfill. Gets the name of the class the static method is called in.
if (!function_exists('get_called_class')) {
/**
* Gets the name of the class the static method is called in.
*
* @return string
*/
function get_called_class() {
$arr = array();
$arrTraces = debug_backtrace();
foreach ($arrTraces as $arrTrace) {
if (!array_key_exists('class', $arrTrace)) {
continue;
}
if (count($arr) == 0) {
$arr[] = $arrTrace['class'];
} else if (get_parent_class($arrTrace['class']) == end($arr)) {
$arr[] = $arrTrace['class'];
}
}
return end($arr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment