tags:

views:

78

answers:

2

Hi

I've been using the following to return filepath and line number of a method

echo (__METHOD__) . "() - ln : " . (__LINE__) . "<br />";

I'd really like to update this to a class (say comment.php)

class comment
{   
    public static function show()
    {
     echo (__METHOD__) . "() - ln : " . (__LINE__) . "<br />";
    }
}

that I can call from anywhere in my development

if(COMMENT) comment::show();

and have it return the filepath of the calling code. I've tried a few variants but I'm missing something. Can you help?

thanks

A: 

Try:

echo __FILE__;
David Grant
+5  A: 

Check out debug_backtrace - this can give you the information about the caller that you need.

For example:

<?php

class comment
{
    public static function show()
    {
            $trace=debug_backtrace();
            print_r($trace);
    }

    public function test()
    {
         comment::show();
    }

}

$comment=new comment();
$comment->test();

Will produce this output

 Array
(
    [0] => Array
        (
            [file] => /tmp/test.php
            [line] => 13
            [function] => show
            [class] => comment
            [type] => ::
            [args] => Array()

        )

    [1] => Array
        (
            [file] => /tmp/test.php
            [line] => 19
            [function] => test
            [class] => comment
            [object] => comment Object ()
            [type] => ->
            [args] => Array()

        )

)

The first element shows the caller details - format this to your liking, and display the whole call stack too if it helps!

Paul Dixon