note 93343 deleted from function.get-called-class by danbrown
| From: | danbrown@php.net | Date: | Fri, 04 Sep 2009 18:00:39 +0000 |
| Subject: | note 93343 deleted from function.get-called-class by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-160222@lists.php.net to get a copy of this message | ||
Note Submitter: mucahitkahveci at yahoo dot com
----
Previous note had some drawbacks. So here is a better one:
<?php
if(!function_exists('get_called_class')) {
function get_called_class() {
$matches=array();
$bt = debug_backtrace();
$l = 0;
do {
$l++;
$lines = file($bt[$l]['file']);
$callerLine = $lines[$bt[$l]['line']-1];
if (!preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l]['function'].'/',
$callerLine,
$matches)) {
preg_match('/([a-zA-Z0-9\_]+)::\$[a-zA-Z0-9\_]+/',
$callerLine,
$matches);
}
if (!isset($matches[1])) $matches[1]=NULL; //for notices
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];
}
}
?>