note 92845 added to function.get-called-class
| From: | tomatrdotje at osu1 dot php dot net | Date: | Tue, 11 Aug 2009 16:18:26 +0000 |
| Subject: | note 92845 added to function.get-called-class | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159338@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 87.124.35.190
Probable Submitter: 195.149.46.150
----
Manual Page -- http://www.php.net/manual/en/function.get-called-class.php
Edit -- https://master.php.net/note/edit/92845
Del: integrated -- https://master.php.net/note/delete/92845/integrated
Del: useless -- https://master.php.net/note/delete/92845/useless
Del: bad code -- https://master.php.net/note/delete/92845/bad+code
Del: spam -- https://master.php.net/note/delete/92845/spam
Del: non-english -- https://master.php.net/note/delete/92845/non-english
Del: in docs -- https://master.php.net/note/delete/92845/in+docs
Del: other reasons-- https://master.php.net/note/delete/92845
Reject -- https://master.php.net/note/reject/92845
Search -- https://master.php.net/manage/user-notes.php