Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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().

like image 980
Gedrox Avatar asked Jul 07 '11 07:07

Gedrox


People also ask

What does the @deprecated annotation do?

The @Deprecated annotation tells the compiler that a method, class, or field is deprecated and that it should generate a warning if someone tries to use it. That's what a deprecated class or method is.

How to deprecate in PHP?

If your functions are part of a class, then you could use trigger_error in the constructor to warn of the deprecation. Alternatively, if the functions are in a single file, then triggering a deprecation warning at the top of the file would show the error whenever the file is included elsewhere.

What is @deprecated annotation in Java?

Annotation Types Used by the Java Language @Deprecated @Deprecated annotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation.

What is deprecated method?

Similarly, when a class or method is deprecated, it means that the class or method is no longer considered important. It is so unimportant, in fact, that it should no longer be used at all, as it might well cease to exist in the future. The need for deprecation comes about because as a class evolves, its API changes.


1 Answers

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.

like image 58
KingCrunch Avatar answered Oct 23 '22 06:10

KingCrunch