note 85802 modified in language.oop5.late-static-bindings by danbrown

From: Date: Thu, 18 Sep 2008 14:56:50 +0000
Subject: note 85802 modified in language.oop5.late-static-bindings by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-144514@lists.php.net to get a copy of this message
get_called_class for PHP < 5.3 <?php /** * Return called class name * * @author Michael Grenier * @param int $i_level optional * @return string */ function get_called_class ($i_level = 1) { $a_debug = debug_backtrace(); $a_called = array(); $a_called_function = $a_debug[$i_level]['function']; for ($i = 1, $n = sizeof($a_debug); $i < $n; $i++) { if (in_array($a_debug[$i]['function'], array('eval')) || strpos($a_debug[$i]['function'], 'eval()') !== false) continue; if (in_array($a_debug[$i]['function'], array('__call', '__callStatic'))) $a_called_function = $a_debug[$i]['args'][0]; if ($a_debug[$i]['function'] == $a_called_function) $a_called = $a_debug[$i]; } if (isset($a_called['object']) && isset($a_called['class'])) return (string)$a_called['class']; $i_line = (int)$a_called['line'] - 1; $a_lines = explode("\n", file_get_contents($a_called['file'])); preg_match("#([a-zA-Z0-9_]+){$a_called['type']} {$a_called['function']}( )*\(#", $a_lines[$i_line], $a_match); unset($a_debug, $a_called, $a_called_function, $i_line, $a_lines); if (sizeof($a_match) > 0) $s_class = (string)trim($a_match[1]); else $s_class = (string)$a_called['class']; if ($s_class == 'self') return get_called_class($i_level + 2); return $s_class; } ?> --was-- get_called_class for PHP < 5.3 /** * Return called class name * * @author Michael Grenier * @param int $i_level optional * @return string */ function get_called_class ($i_level = 1) { $a_debug = debug_backtrace(); $a_called = array(); $a_called_function = $a_debug[$i_level]['function']; for ($i = 1, $n = sizeof($a_debug); $i < $n; $i++) { if (in_array($a_debug[$i]['function'], array('eval')) || strpos($a_debug[$i]['function'], 'eval()') !== false) continue; if (in_array($a_debug[$i]['function'], array('__call', '__callStatic'))) $a_called_function = $a_debug[$i]['args'][0]; if ($a_debug[$i]['function'] == $a_called_function) $a_called = $a_debug[$i]; } if (isset($a_called['object']) && isset($a_called['class'])) return (string)$a_called['class']; $i_line = (int)$a_called['line'] - 1; $a_lines = explode("\n", file_get_contents($a_called['file'])); preg_match("#([a-zA-Z0-9_]+){$a_called['type']} {$a_called['function']}( )*\(#", $a_lines[$i_line], $a_match); unset($a_debug, $a_called, $a_called_function, $i_line, $a_lines); if (sizeof($a_match) > 0) $s_class = (string)trim($a_match[1]); else $s_class = (string)$a_called['class']; if ($s_class == 'self') return get_called_class($i_level + 2); return $s_class; } http://php.net/manual/en/language.oop5.late-static-bindings.php

« previous php.notes (#144514) next »