문제

Is it possible to get information (filename, line, function ...) of the calling function by using late static binding?

<?php
class Log{
    public static function write($msg){
        $line = ??;
        $msg = date('Y-m-d H:i:s').' '.$line.' '.$msg;
    }
}

Log::write("wuhuu!"); // write new log entry including >>this<< line/filename/..
?>

In former times I used debug_backtrace() or new \Exception + getTrace(). Would it be possible (or easier) to use some great super-special late static binding feature-keywords/functions?

도움이 되었습니까?

해결책

Unfortunately debug_backtrace is probably your best best, although this is quite inefficient.

The alternative would be to pass the line and file back to your log method...

Log::write("wuhuu!", __LINE__, __FILE__);

It's a pain in the ass but I can't see another solution.

다른 팁

You should check the XDebug extension:

xdebug_call_file(), xdebug_call_line(), xdebug_call_class(), xdebug_call_function()

These kind of function (XDebug, debug_backtrace()) are not recommended in a production environment.

Yea,

PHP provides a large number of predefined constants to any script which it runs. Many of these constants, however, are created by various extensions, and will only be present when those extensions are available, either via dynamic loading or because they have been compiled in.

There are eight magical constants that change depending on where they are used. For example, the value of LINE depends on the line that it's used on in your script. These special constants are case-insensitive and are as follows:

_LINE_, _CLASS_, _METHOD_, etc

http://php.net/manual/en/language.constants.predefined.php

But you still need give that data to the method like log(_LINE_,_METHOD_).

for global warnings you can catch those with http://www.php.net/manual/en/function.set-error-handler.php

other then that ..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top