How to get the line number in PHP?

3

How can I be getting the line number on which the method was executed for example?

class.php

Class Example
{
    function methodExample()
    {
        echo __LINE__;
    }
}

index.php

include "class.php";

Example::methodExample(); // 5

The use of _LINE_ does not do what I want, since it displays the line where it was inserted and not where the method was called.

    
asked by anonymous 02.07.2016 / 02:24

1 answer

4

Pass the current row number as argument to the method (it must have a parameter to receive this argument). Or use debug_backtrace() to get all (slower) call information.

Class Example {
    function methodExample($line) {
        echo $line . "\n";
    }
    function methodExample2() {
        echo debug_backtrace()[0]["line"];
    }
}
Example::methodExample(__LINE__);
Example::methodExample2();

See working on ideone .

    
02.07.2016 / 02:44