Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the caller's method name

Tags:

php

So, I'm looking for how to get the caller's method name. I never saw it and I don't know if it exists.

Example:

<?php 
  class father {
    public function db_select(){
      echo method that call me;
    }
  }

  class plugin extends father {
    public function select_plugin(){
      $query = $this->db_select();
    }
   }

?>

All I want is to write the method's name that calls db_select() method, inside db_select().

like image 313
Jean Victor Avatar asked Dec 18 '15 16:12

Jean Victor


1 Answers

I think debug_backtrace could do the trick.

It gives you a backtrace of a function call. Observe this result then you'll have to figure out how to grab the function name you want. But the information is there.

<?php 
  class father {
    public function db_select(){
      echo debug_backtrace()[1]['function'];
      print_r(debug_backtrace());     
    }
  }

  class plugin extends father {
    public function select_plugin(){
      $query = $this->db_select();
    }
   }

?>
like image 140
Dan Avatar answered Sep 27 '22 22:09

Dan