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);

  • may be own file parser helps you...
  • deprecated mean that in next version this function will be removed from code... in this case you do not need to think about E_DEPRECATED
  • 链接地址: http://www.djcxy.com/p/8298.html

    上一篇: 全球定位系统

    下一篇: 使用@deprecated批注自动弃用PHP方法的警告