Published
Dominik Chrástecký - Blog New in PHP 8.5: Attributes on Constants New in PHP 8.5: Attributes on Constants- 1 min read
New in PHP 8.5: Attributes on Constants

PHP 8.5 introduces support for attributes on non-class constants. This article walks you through everything you need to know about this new feature!
This change is quite straightforward, so this won’t be a long article. PHP 8.5 adds support for annotating non-class, compile-time constants with attributes. Compile-time constants are those defined using the const keyword, not the define() function.
Attributes can now include Attribute::TARGET_CONSTANT among their valid targets. Additionally, as the name suggests, Attribute::TARGET_ALL now includes constants as well. The ReflectionConstant class has been updated with a new method, getAttributes(), to support retrieving these annotations.
One particularly useful aspect of this change is that the built-in #[Deprecated] attribute can now be applied to compile-time constants.
As promised, this was a short post, since the change is relatively simple. See you next time—hopefully with a more exciting new feature in PHP 8.5!
- New in PHP 8.5: Marking Return Values as Important
- New in PHP 8.5: Levenshtein Comparison for UTF-8 Strings
- New in PHP 8.5: Final Promoted Properties
- New in PHP 8.5: The Pipe Operator
- New in PHP 8.5: Asymmetric Visibility for Static Properties
- New in PHP 8.5: Closures as Constant Expressions
- New in PHP 8.5: Small Features, Big Impact