note 92572 deleted from function.get-called-class by danbrown
| From: | danbrown@php.net | Date: | Fri, 08 Jun 2012 20:18:45 +0000 |
| Subject: | note 92572 deleted from function.get-called-class by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188494@lists.php.net to get a copy of this message | ||
Note Submitter: steve at visual77 dot com
----
I've furthered enhanced the attempts to fix get_called_class() for PHP 5.2 Using a class and
some static properties, we can keep track of how many times it has already processed the current
line, so multiple calls on the same line will still function properly.
<?php
if(!function_exists('get_called_class')) {
class class_tools {
static $i = 0;
static $fl = null;
static function get_called() {
$bt = debug_backtrace();
if(self::$fl == $bt[2]['file'].$bt[2]['line']) {
self::$i++;
} else {
self::$i = 0;
self::$fl = $bt[2]['file'].$bt[2]['line'];
}
$lines = file($bt[2]['file']);
preg_match_all('/([a-zA-Z0-9\_]+)::'.$bt[2]['function'].'/',
$lines[$bt[2]['line']-1],
$matches);
return $matches[1][self::$i];
}
}
function get_called_class() {
return class_tools::get_called_class();
}
}
?>