Automatic deprecated warnings for PHP methods with @deprecated annotation
What are possibilities to implement helper that will raise error log with level E_DEPRECATED
( E_USER_DEPRECATED
in fact) when class method with annotation @deprecated
is called?
For example for the code
/**
* @deprecated
*/
public function main()
{}
when calling the method $obj->main()
the deprecated warning would be raised.
And yes, I know I could add a warning using code line trigger_error()
.
In short: Put trigger_error()
at the beginning of the method.
Long: You need to reflect the class, retrieve the DocComment, parse it and extract the @deprecated
-tag. The problem is, that you must do this on every method call, and even if it there exists an easy way to catch every call, it would be a huge overhead.
If you are still interested in an answer:
$trace = debug_backtrace();
$trace = $trace[0];
Helper::logToFile('called deprecated method '.__ FUNCTION __.' on line '.$trace['line'].' in file '.$trace['file'], 'deprecated');
Where the log to file method could look like:
$text .= "n";
$file = fopen('log/deprecated', 'a+');
fputs($file, $text, strlen($text));
fclose($file);
E_DEPRECATED
上一篇: 全球定位系统