mefisto Posted May 7, 2010 Report Share Posted May 7, 2010 Baigi vajag funkciju get_called_class(), bet man pieejams ir tikai php 5.1.x ( dāvāts zirgs , zobos neskatīties ). Vai ir kādi varianti kā to varētu implementēt ? Googlē atradu šādu variantu : http://www.sitepoint.com/forums/showthread.php?t=605318 Bet tas šķiet nedarbojas ar statiskām funckijām. Quote Link to comment Share on other sites More sharing options...
codez Posted May 7, 2010 Report Share Posted May 7, 2010 nestatiskā veidā gan šķiet pietiktu ar: get_class($this) lai iegūtu izsaukto klasi. Quote Link to comment Share on other sites More sharing options...
mefisto Posted May 7, 2010 Author Report Share Posted May 7, 2010 nevermind. Šitas variants nostrādāja: if(!function_exists('get_called_class')) { class class_tools { static $i = 0; static $fl = null; static function get_called_class() { $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(); } } Drošvien ka biju sapisies meistarībā. Quote Link to comment Share on other sites More sharing options...
codez Posted May 7, 2010 Report Share Posted May 7, 2010 (edited) Šeit ir tāda, kā lai latviski izsakās, metode, kura pēc backtrace meklē vietu php failā, kur statiski metode tika izsaukta, lai uzzinātu klases nosaukumu: no http://www.php.net/manual/en/function.get-called-class.php#93799 if(!function_exists('get_called_class')) { function get_called_class($bt = false,$l = 1) { if (!$bt) $bt = debug_backtrace(); if (!isset($bt[$l])) throw new Exception("Cannot find called class -> stack level too deep."); if (!isset($bt[$l]['type'])) { throw new Exception ('type not set'); } else switch ($bt[$l]['type']) { case '::': $lines = file($bt[$l]['file']); $i = 0; $callerLine = ''; do { $i++; $callerLine = $lines[$bt[$l]['line']-$i] . $callerLine; } while (stripos($callerLine,$bt[$l]['function']) === false); preg_match('/([a-zA-Z0-9\_]+)::'.$bt[$l]['function'].'/', $callerLine, $matches); if (!isset($matches[1])) { // must be an edge case. throw new Exception ("Could not find caller class: originating method call is obscured."); } switch ($matches[1]) { case 'self': case 'parent': return get_called_class($bt,$l+1); default: return $matches[1]; } // won't get here. case '->': switch ($bt[$l]['function']) { case '__get': // edge case -> get class of calling object if (!is_object($bt[$l]['object'])) throw new Exception ("Edge case fail. __get called on non object."); return get_class($bt[$l]['object']); default: return $bt[$l]['class']; } default: throw new Exception ("Unknown backtrace method type"); } } } EDIT: Jā šķiet, ka tavā variantā arī meklē koda faila rindiņu, lai noskaidrotu klases nosaukumu. Edited May 7, 2010 by codez Quote Link to comment Share on other sites More sharing options...
mefisto Posted May 7, 2010 Author Report Share Posted May 7, 2010 Naksies turēt to failu mazu =/ Sucks ka nav labāka/ātrāka varianta. Quote Link to comment Share on other sites More sharing options...
bubu Posted May 7, 2010 Report Share Posted May 7, 2010 Var jau nolasīto informāciju kešot. Ja faila modificēšanas datums nav mainījies no piekešotās vērtības saglabāšanas brīža, tad ņemam keša vērtību. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.