Know line and file that is executing a method

4

I want to know if it is possible to know the line and file that a method is executing. For example, I have a class Log , with it I make log records of the users.

Log.php

<?php
/**
* Classe de Logs
*/
class Log
{
   var $con;

   private $error, $orig;

   function __construct($userid = NULL) {
      global $con;
      $this->con = $con;
      $this->obs = '';
   }

   function SetError($error, $type = 1, $concat = FALSE, $register = TRUE){
      $this->error = ($concat ? $this->error."\n ".$error : $error);
      $this->orig = 'Arquivo: '.__FILE__."\n Linha: ".__LINE__;
      if($register) $this->RegisterLog($error);
      return FALSE;
   }

Arte.php (line 85)

  } else return $this->SetError('Não foi possível carregar os produtos.');

NOTE: CLASS% with% EXTEND CLASS ARTE

When you enter this line, I would like the LOG variable to have the following value:

  

File: C: \ xampp \ htdocs \ project \ app \ class \ Arte.php Line: 85

But the value comes:

  

File: C: \ xampp \ htdocs \ project \ app \ class \ Log.php Line: 19

Is it possible to do this? Are there any other alternatives I can do?

PS.: If possible I would like to not pass this information through parameters, as I would have to move a lot.

    
asked by anonymous 06.01.2015 / 20:00

1 answer

7

You can do this by using the debug_backtrace () function:

class Caller extends Log {

    function test() {
        // Chama a função na classe log
        parent::setLog();
    }

}

class Log {
    // Metodo de log
    public function setLog() {
        var_dump(debug_backtrace());
    }

}

$caller = new Caller();
$caller->test();

Output:

array (size=2)
  0 => 
    array (size=7)
      'file' => string '/home/edi/NetBeansProjects/zion/testes/test.php' (length=47)
      'line' => int 7
      'function' => string 'setLog' (length=6)
      'class' => string 'Log' (length=3)
      'object' => 
        object(Caller)[1]
      'type' => string '->' (length=2)
      'args' => 
        array (size=0)
          empty
  1 => 
    array (size=7)
      'file' => string '/home/edi/NetBeansProjects/zion/testes/test.php' (length=47)
      'line' => int 24
      'function' => string 'test' (length=4)
      'class' => string 'Caller' (length=6)
      'object' => 
        object(Caller)[1]
      'type' => string '->' (length=2)
      'args' => 
        array (size=0)
          empty

link

    
06.01.2015 / 20:46