Posted By

ryantxr on 06/07/12


Tagged

backtrace


Versions (?)

Calling Method


 / Published in: PHP
 

Shows how to get the name of the calling function. Use this to implement a log method start.

  1. <?php
  2.  
  3. class Base{
  4. function logMethodStart(){
  5. $trace = debug_backtrace();
  6. //var_dump($trace);
  7. $method = '';
  8. if ( count($trace) > 1 ){
  9. $data = $trace[1];
  10. if ( ! empty($data['class']) ){
  11. $method .= $data['class'].'::';
  12. }
  13. $method .= $data['function'];
  14. }
  15. echo "Method = \"". $method. "\"";
  16. echo "\n";
  17. }
  18. }
  19.  
  20. class Outer extends Base {
  21.  
  22. function mytest(){
  23.  
  24. $this->logMethodStart();
  25.  
  26. }
  27.  
  28. }
  29.  
  30.  
  31. function functest(){
  32. Base::logMethodStart();
  33. }
  34.  
  35. $o = new Outer();
  36. $o->mytest();
  37.  
  38. functest();
  39.  
  40. Base::logMethodStart();
  41.  
  42. ?>

Report this snippet  

You need to login to post a comment.