note 97344 added to function.get-called-class
| From: | thomas at weblizards dot de | Date: | Thu, 15 Apr 2010 10:10:21 +0000 |
| Subject: | note 97344 added to function.get-called-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168261@lists.php.net to get a copy of this message | ||
I added a little cache to Laurence's work, so the function doesn't have to parse the
sourcefile each time it is called:
<?
$get_called_class_cache_file = "/path/to/writeable/file.php";
if(file_exists($get_called_class_cache_file)) {
$GLOBALS["get_called_classes"] = include_once($get_called_class_cache_file);
} else {
$GLOBALS["get_called_classes"] = array();
file_put_contents($get_called_class_cache_file,'<? return
'.var_export($GLOBALS["get_called_classes"], 1).';');
}
function get_called_class($bt = false, $l = 1) {
global $get_called_class_cache_file;
if (!$bt) $bt = debug_backtrace();
if (!isset($bt[$l])) throw new Exception("Cannot find called class -> stack level too
deep.");
if (!isset($bt[$l]["type"])) throw new Exception ("type not set");
switch ($bt[$l]['type']) {
case "::":
if (array_key_exists($bt[$l]['file'], $GLOBALS["get_called_classes"])) return
$GLOBALS["get_called_classes"][$bt[$l]['file']];
$lines = file($bt[$l]['file']);
$i = 0;
$callerLine = '';
$class_name = "";
do {
$i++;
$callerLine = $lines[$bt[$l]['line']-$i] . $callerLine;
} while (stripos($callerLine,$bt[$l]['function']) === false);
$matches = array();
preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l]['function'].'/',
$callerLine, $matches);
if (!isset($matches[1])) throw new Exception ("Could not find caller class: originating
method call is obscured."); // must be an edge case.
switch ($matches[1]) {
case 'self':
case 'parent':
$class_name = get_called_class($bt,$l+1);
break;
default:
$class_name = $matches[1];
break;
}
$GLOBALS["get_called_classes"][$bt[$l]['file']] = $class_name;
file_put_contents($get_called_class_cache_file,'<? return
'.var_export($GLOBALS["get_called_classes"], 1).';');
return $class_name;
case '->':
switch ($bt[$l]['function']) {
case '__get': // edge case -> get class of calling object
if (!is_object($bt[$l]['object'])) throw new Exception ("Edge case fail. __get
called on non object.");
return get_class($bt[$l]['object']);
default: return $bt[$l]['class'];
}
default: throw new Exception ("Unknown backtrace method type");
}
}
?>
----
Server IP: 212.124.37.9
Probable Submitter: 80.153.227.197
----
Manual Page -- http://www.php.net/manual/en/function.get-called-class.php
Edit -- https://master.php.net/note/edit/97344
Del: integrated -- https://master.php.net/note/delete/97344/integrated
Del: useless -- https://master.php.net/note/delete/97344/useless
Del: bad code -- https://master.php.net/note/delete/97344/bad+code
Del: spam -- https://master.php.net/note/delete/97344/spam
Del: non-english -- https://master.php.net/note/delete/97344/non-english
Del: in docs -- https://master.php.net/note/delete/97344/in+docs
Del: other reasons-- https://master.php.net/note/delete/97344
Reject -- https://master.php.net/note/reject/97344
Search -- https://master.php.net/manage/user-notes.php