danielescherzer/define-deprecated

Allow defining deprecated global constants at runtime without dropping PHP <8.5 support

Maintainers

Package info

github.com/DanielEScherzer/define-deprecated

pkg:composer/danielescherzer/define-deprecated

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-04-10 17:07 UTC

This package is auto-updated.

Last update: 2026-04-10 17:13:51 UTC


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 );