Automatic deprecated warnings for PHP methods with @deprecated annotation

What are the possibilities of implementing an assistant that will lead to the appearance of an error log with the level E_DEPRECATED ( E_USER_DEPRECATED ) when the class method is called with the annotation @deprecated ?

For example, for code

 /** * @deprecated */ public function main() {} 

when calling the $obj->main() method, an obsolete warning will be $obj->main() .

And yes, I know that I could add a warning using the trigger_error() line of code.

+7
source share
4 answers

In short: put trigger_error() at the beginning of the method.

Long: you need to flip the class, get a DocComment, parse it and extract @deprecated -tag. The problem is that you have to do this every time you call a method, and even if it is an easy way to catch every call, it will be a huge overhead.

+4
source

If you are still interested in the 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 method for the file might look:

 $text .= "\n"; $file = fopen('log/deprecated', 'a+'); fputs($file, $text, strlen($text)); fclose($file); 
+1
source
  • may be your own file parser to help you ...
  • deprecated means that in the next version this function will be removed from the code ... in this case you do not need to think about E_DEPRECATED
-one
source

Addendum can help, it adds annotations to PHP.

-one
source

All Articles