note 93331 deleted from function.get-called-class by danbrown

From: Date: Fri, 08 Jun 2012 20:18:33 +0000
Subject: note 93331 deleted from function.get-called-class by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188492@lists.php.net to get a copy of this message
Note Submitter: mucahitkahveci at yahoo dot com ---- Addition to tom at r dot je: It is fine but does not work for method calls like: <?php class Car { static function getClass() { echo get_called_class(); } } $method='getClass'; Car::$method(); //prints nothing ?> So I developed to meet also this issue, and add one extra line not to cause notice errors: <?php if(!function_exists('get_called_class')) { function get_called_class() { $matches=array(); $bt = debug_backtrace(); $l = 0; do { $l++; if(isset($bt[$l]['class']) AND !empty($bt[$l]['class'])) { return $bt[$l]['class']; } $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); } } while ($matches[1] == 'parent' && $matches[1]); return $matches[1]; } } ?>

« previous php.notes (#188492) next »