Find out which class called a method in another class

后端 未结 8 2107
时光说笑
时光说笑 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:59

    This function does the job without debug_backtrace :

    /*
    usage :
    some code...
    getRealCallClass(__FUNCTION__);
    some code...
    */
    
    function getRealCallClass($functionName) //Parameter value must always be __FUNCTION__
    {
      try
       {
         throw new exception();
       }
      catch(exception $e)
       {
         $trace = $e->getTrace();
         $bInfunction = false;
         foreach($trace as $trace_piece)
          {
              if ($trace_piece['function'] == $functionName)
               {
                 if (!$bInfunction)
                  $bInfunction = true;
               }
              elseif($bInfunction) //found !!!
               {
                 return $trace_piece['class'];
               }
          }
       }
    }
    

提交回复
热议问题