note 93331 added to function.get-called-class

From: Date: Thu, 03 Sep 2009 14:21:02 +0000
Subject: note 93331 added to function.get-called-class
Groups: php.notes 
Request: Send a blank email to php-notes+get-160193@lists.php.net to get a copy of this message
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]; } } ?> ---- Server IP: 160.75.100.7 Probable Submitter: 85.105.22.157 ---- Manual Page -- http://www.php.net/manual/en/function.get-called-class.php Edit -- https://master.php.net/note/edit/93331 Del: integrated -- https://master.php.net/note/delete/93331/integrated Del: useless -- https://master.php.net/note/delete/93331/useless Del: bad code -- https://master.php.net/note/delete/93331/bad+code Del: spam -- https://master.php.net/note/delete/93331/spam Del: non-english -- https://master.php.net/note/delete/93331/non-english Del: in docs -- https://master.php.net/note/delete/93331/in+docs Del: other reasons-- https://master.php.net/note/delete/93331 Reject -- https://master.php.net/note/reject/93331 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160193) next »