Jump to content
php.lv forumi

get_called_class() uz vecāka php5


mefisto

Recommended Posts

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ā.

Link to comment
Share on other sites

Š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 by codez
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...