Automatic deprecated warnings for PHP methods with @deprecated annotation

Viewed 7593

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

5 Answers

There is an RFC for it, but under discussion: #[Deprecated] Attribute. If accepted, you will be able to do this, for example:

#[Deprecated("use split() instead")]
function explode() {}

When you call it, it will emit a deprecation notice, like so:

Deprecated: Function explode() is deprecated, use split() instead

Maybe we could get this in PHP 8.2? I hope so!

Related