← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Constants (define vs const)

PHP PHP 5.0+ Beginner
debt(d3/e3/b3/t5)
d3 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'default linter catches the common case' (d3). The detection_hints list phpcs, phpstan, and rector — all standard PHP tooling that catches misuse patterns like define() where const is preferred, magic strings that should be constants, and naming convention violations. These are common-case catches via standard static analysis, not specialist one-off tools.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix (replace pattern with safer alternative)' (e3). The quick_fix describes replacing define() with class constants or PHP 8.1 enums, which is a straightforward substitution. Rector can automate many of these replacements. However, replacing magic numbers or strings scattered across a codebase requires a systematic find-and-replace within a component, fitting e3 rather than a single one-line patch.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The choice of define() vs const is mostly scoped: class constants affect the class and its consumers, and global define() constants add a small tax to the files that use them. The applies_to scope covers web, cli, and queue-worker contexts, but the structural impact is localised — it does not reshape the whole architecture or impose a persistent productivity drag across many work streams.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field explicitly states the canonical wrong belief: developers treat define() and const as fully interchangeable, not realising that const is compile-time (usable in class bodies and namespaces) while define() is runtime (supports dynamic names, works inside conditionals). This is a documented, well-known PHP gotcha that intermediate developers eventually learn but beginners consistently get wrong.

About DEBT scoring →

Also Known As

PHP constants define() const keyword PHP const

TL;DR

Named immutable values in PHP — const is compile-time and class-scoped; define() is runtime and supports dynamic names.

Explanation

PHP has two ways to define constants: const NAME = value (compile-time, usable in class bodies, interfaces, and at the top level, but not inside functions or conditionals) and define('NAME', value) (runtime, supports variable names, works anywhere). Class constants (const) support visibility modifiers since PHP 7.1 and typed constants since PHP 8.3. Use const for class-level constants and top-level application configuration; use define() when the constant name or value must be computed at runtime. Constants are globally accessible by name and cannot be redefined — prefer class constants or enums for namespaced, typed constant groups.

Common Misconception

define() and const are fully interchangeable for declaring constants. const is resolved at compile time and can be used in class bodies; define() is resolved at runtime, supports dynamic names, and works inside conditionals — const is preferred for class and namespace constants.

Why It Matters

Constants defined with const or define() cannot be changed at runtime — they communicate intent clearly and prevent accidental reassignment of values that should never vary.

Common Mistakes

  • Using define() inside functions or conditionally — prefer const at the top level for clarity and performance.
  • Class constants not declared final in PHP 8.1 — subclasses can override them, violating the 'constant' contract.
  • Using magic numbers instead of named constants — the meaning is lost and updates require searching the whole codebase.
  • Naming constants with camelCase instead of SCREAMING_SNAKE_CASE — breaks PHP convention and reduces readability.

Code Examples

✗ Vulnerable
// Magic number — meaning unknown:
if ($status === 3) { /* What is 3? */ }

// Named constant:
const ORDER_STATUS_SHIPPED = 3;
if ($status === ORDER_STATUS_SHIPPED) { /* Clear */ }

// Non-final class constant — overrideable:
class Config {
    const TIMEOUT = 30; // Should be: final const TIMEOUT = 30; (PHP 8.1+)
}
✓ Fixed
// define() — runtime, works anywhere
define('MAX_UPLOAD', 10 * 1024 * 1024);
define('APP_ENV', getenv('APP_ENV') ?: 'production');

// const — compile-time, preferred in classes/namespaces
class Order {
    const STATUS_PENDING   = 'pending';
    const STATUS_PAID      = 'paid';
    const STATUS_CANCELLED = 'cancelled';
}
echo Order::STATUS_PAID;

// PHP 8.3 — typed class constants
class Config {
    const string VERSION     = '2.4.1';
    const int    MAX_RETRIES = 3;
}

// Useful built-in constants:
// PHP_EOL, PHP_INT_MAX, PHP_INT_SIZE
// DIRECTORY_SEPARATOR, PATH_SEPARATOR

Tags


Added 15 Mar 2026
Edited 22 Mar 2026
Views 35
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 1 ping F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 10 Perplexity 4 Unknown AI 4 Ahrefs 3 SEMrush 3 Scrapy 3 Google 2 Claude 2 ChatGPT 2 Bing 1 Meta AI 1 PetalBot 1
crawler 31 crawler_json 4 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use class constants (const STATUS_ACTIVE = 'active') scoped to the class they belong to; use PHP 8.1 enums instead of string constants for type safety; avoid global define() constants except for truly global config
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
define() for domain constants that should be class constants; repeated magic string that should be a constant; constant not used in same class it's defined
Auto-detectable: ✓ Yes phpcs phpstan rector
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✓ Auto-fixable Fix: Low Context: File


✓ schema.org compliant