danielescherzer / define-deprecated
Allow defining deprecated global constants at runtime without dropping PHP <8.5 support
Package info
github.com/DanielEScherzer/define-deprecated
pkg:composer/danielescherzer/define-deprecated
Requires
- php: ^8.1
Requires (Dev)
- danielescherzer/common-phpcs: 0.0.2
- php-parallel-lint/php-parallel-lint: ^1.4
- phpunit/phpunit: ^10.5
README
About
This composer library implements a way to define constants at runtime that
should be marked as deprecated. On PHP 8.4 and below, the constant is defined
using define(), while on PHP 8.5 and above, the constant is
defined using eval() with the
#[\Deprecated] attribute applied.
For background, see this blog post - other than eval() there
is no easy way to make use of the ability to deprecate constants on PHP 8.5
without also requiring PHP 8.5 as the minimum supported version of the code.
Installation
The library is meant to be installed via composer, e.g. using
composer require danielescherzer/define-deprecated.
It supports (and is tested against) PHP 8.1 and later.
Usage
The primary entrypoint of this library is the function
\DanielEScherzer\DefineDeprecated\define_deprecated() which accepts a name,
value, deprecation message, and deprecation version. It wraps around the
\DanielEScherzer\DefineDeprecated\ConstantDefiner class which holds the
actual logic; since PHP does not yet support function autoloading, the
define_deprecated() function is always loaded.
To use it:
<?php use function DanielEScherzer\DefineDeprecated\define_deprecated; define_deprecated( 'MY_CONSTANT', true, 'Do not use this', '1.0' ); // This will output "bool(true)", and on PHP 8.5+ also emit deprecation warnings var_dump( MY_CONSTANT );