note 107445 deleted from function.get-called-class by danbrown
| From: | danbrown@php.net | Date: | Fri, 08 Jun 2012 20:17:48 +0000 |
| Subject: | note 107445 deleted from function.get-called-class by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188490@lists.php.net to get a copy of this message | ||
Note Submitter: dsaa at dubli dot com
----
The workarounds to use get_called_class in PHP versions previous to 5.3 didn't quite work for
me when the class name in my call was contained in a variable, so I did a couple of modifications to
the workarounds proposed.
<?php
/**
* workaround until update to PHP 5.3 takes place
* (Don't make more than one call in the same line, or it will break!!!).
*/
if (!function_exists('get_called_class')):
function get_called_class()
{
$bt = debug_backtrace();
$l = count($bt) - 1;
$matches = array();
while(empty($matches) && $l > -1){
$lines = file($bt[$l]['file']);
$callerLine = $lines[$bt[$l]['line']-1];
preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l--]['function'].'/',
$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);
}
return $matches[1];
}
endif;
?>