note 107445 added to function.get-called-class

From: Date: Thu, 09 Feb 2012 18:22:51 +0000
Subject: note 107445 added to function.get-called-class
Groups: php.notes 
Request: Send a blank email to php-notes+get-185943@lists.php.net to get a copy of this message
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; ?> ---- Server IP: 69.147.83.197 Probable Submitter: 66.55.175.74 ---- Manual Page -- http://www.php.net/manual/en/function.get-called-class.php Edit -- https://master.php.net/note/edit/107445 Del: integrated -- https://master.php.net/note/delete/107445/integrated Del: useless -- https://master.php.net/note/delete/107445/useless Del: bad code -- https://master.php.net/note/delete/107445/bad+code Del: spam -- https://master.php.net/note/delete/107445/spam Del: non-english -- https://master.php.net/note/delete/107445/non-english Del: in docs -- https://master.php.net/note/delete/107445/in+docs Del: other reasons-- https://master.php.net/note/delete/107445 Reject -- https://master.php.net/note/reject/107445 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185943) next »