note 92845 deleted from function.get-called-class by danbrown
| From: | danbrown@php.net | Date: | Fri, 08 Jun 2012 20:18:37 +0000 |
| Subject: | note 92845 deleted from function.get-called-class by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188493@lists.php.net to get a copy of this message | ||
Note Submitter: tom at r dot je
----
I've further extended spidgorny at gmail dot com's 5.2 get_called_class() solution to also
account for self:: calls. It works backwards up the file to find the class definition.
<?php
if(!function_exists('get_called_class')) {
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);
if ($matches[1] == 'self') {
$line = $bt[$l]['line']-1;
while ($line > 0 && strpos($lines[$line], 'class') === false) {
$line--;
}
preg_match('/class[\s]+(.+?)[\s]+/si', $lines[$line], $matches);
}
}
while ($matches[1] == 'parent' && $matches[1]);
return $matches[1];
}
}
?>
Not pretty but works until servers can be upgraded.