Find out which class called a method in another class

后端 未结 8 2094
时光说笑
时光说笑 2020-11-27 06:10

Is there a way in PHP to find out what object called what method in another object.

Exmaple:

class Foo
{
  public function __construct()
  {
    $bar         


        
8条回答
  •  無奈伤痛
    2020-11-27 06:57

    Here is one liner solution

    list(, $caller) = debug_backtrace(false, 2);
    

    As of PHP7 this won't work based on the docs: http://php.net/manual/en/function.list.php as we cannot have empty properties, here is a small update:

    list($childClass, $caller) = debug_backtrace(false, 2);
    

提交回复
热议问题