php 獲取函式被呼叫位置

G8bao7發表於2016-11-03

# get_caller.php
  1. <?php

  2. function get_caller_info($logFile = "call_info.log"){
  3.     $callInfos = array();
  4.     $callFuncs = array();
  5.     $curTime = date('y-m-d h:i:s',time());
  6.     $traces = debug_backtrace();
  7.     $j = 0;
  8.     $length = count($traces)-1;
  9.     for ($i = $length; $i >=0; $i--){
  10.         $trace = $traces[$i];
  11.         $file = $trace['file'];
  12.         $line = $trace['line'];
  13.         $func = $trace['function'];
  14.         $args = $trace['args'];
  15.         $argsv = join(",", $args);
  16.         $tInfo = $curTime." ".$file."(".$line.")->".$func."(".$argsv.")";
  17.         array_push($callInfos, $tInfo);
  18.         $tFunc = $file."->".$func."()";
  19.         array_push($callFuncs, $tFunc);
  20.         $j++;
  21.     }

  22.     $callInfoStr = join(";",$callInfos);
  23.     $callFuncStr = join(";",$callFuncs);
  24.     $callFuncMd5 = $curTime." ".md5($callFuncStr);
  25.     file_put_contents($logFile, $callFuncMd5."\n",FILE_APPEND);

  26.     $logFileMd5 = $logFile.".md5";
  27.     $contMd5 = $callFuncMd5.":".$callInfoStr;
  28.     file_put_contents($logFileMd5, $contMd5."\n",FILE_APPEND);
  29.     
  30.     return $callInfos;
  31. }

  32. ?>

# test_caller.php

點選(此處)摺疊或開啟

  1. <?php
  2. require dirname(__FILE__).'/get_caller.php';

  3. function b($pa,$pb){
  4.     $t=$pa.$pb;
  5.     get_caller_info();
  6.     return $t;
  7. }

  8. function c($pa,$pb,$pc){
  9.     $t=$pa.$pb.$pc;
  10.     get_caller_info();
  11.     return $t;
  12. }
  13. function a() {
  14.     b('b1','b2');
  15.     for ($i = 0; $i <10; $i++){
  16.     c('c'.$i,'c2','c3');
  17.     }
  18. }

  19. a();

  20. ?>



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26250550/viewspace-2127637/,如需轉載,請註明出處,否則將追究法律責任。

相關文章