Last active
April 19, 2023 21:55
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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