PHP Class Constants

La Lakshmi Priya Updated 24 Oct 2026
7 min read ·Lesson 186 of 187

What Are Class Constants?

In simple terms: A class constant is a fixed value that belongs to a class — a value that never changes while the program runs, such as a tax rate, a maximum length, or a status name. Instead of scattering the literal 18 or 'pending' throughout the code, you give it a meaningful name in the class it belongs to: Tax::GST_RATE, Order::STATUS_PENDING.

Technically: Class constants are declared with the const keyword inside a class, interface, trait (PHP 8.2+), or enum. They are accessed with the scope resolution operator :: — ClassName::NAME from outside, self::NAME or static::NAME inside. Their values are evaluated from constant expressions. Since PHP 7.1 they can have visibility, since PHP 8.1 they can be final, and since PHP 8.3 they can have a type.

Constants vs Global Constants vs Properties

Class ConstantGlobal Constant (define/const)Property
Belongs toA classThe whole programAn object (or class, if static)
Can change?NoNoYes (unless readonly)
AccessOrder::STATUS_PAIDAPP_VERSION$order->status
GroupingGrouped with related codeGlobal namespacePer object

Declaring and Using Class Constants

Example

<?php
class Order
{
    public const STATUS_PENDING = 'pending';
    public const STATUS_PAID = 'paid';
    public const STATUS_SHIPPED = 'shipped';

    public const MAX_ITEMS = 50;
    public const RETURN_WINDOW_SECONDS = 7 * 24 * 60 * 60;

    private string $status = self::STATUS_PENDING;

    public function markAsPaid(): void
    {
        $this->status = self::STATUS_PAID;
    }

    public function status(): string
    {
        return $this->status;
    }
}

$order = new Order();
echo $order->status() . "\n";

$order->markAsPaid();
var_dump($order->status() === Order::STATUS_PAID);

echo "Max items: " . Order::MAX_ITEMS . "\n";
echo "Return window: " . Order::RETURN_WINDOW_SECONDS / 86400 . " days\n";

Output

pending
bool(true)
Max items: 50
Return window: 7 days

How It Works

  1. const declares a constant; by convention, names are written in UPPER_SNAKE_CASE.
  2. Inside the class, self::STATUS_PAID refers to the constant; outside, Order::STATUS_PAID.
  3. Constants can be used as property defaults (= self::STATUS_PENDING), which is not possible with function calls.
  4. RETURN_WINDOW_SECONDS uses a constant expression (7 * 24 * 60 * 60); the expression documents how the value was derived.

Why Constants Help

  • No magic values: Order::STATUS_PAID explains itself; 'paid' scattered in fifty places invites typos ('payed').
  • One place to change: if the return window changes to 10 days, only the constant changes.
  • IDE support: editors autocomplete constant names and find all usages.

Constant Expressions

Constant values can use literals, arithmetic, string concatenation, arrays, other constants, and (since PHP 8.1) enum cases — but no function calls and no object creation:

<?php
class Tax
{
    public const GST_STANDARD = 18;
    public const GST_RATES = [
        'books' => 0,
        'stationery' => 12,
        'electronics' => self::GST_STANDARD,
    ];
    public const CURRENCY = 'INR';
    public const LABEL = 'GST (' . self::CURRENCY . ')';
}

echo Tax::LABEL . "\n";
echo Tax::GST_RATES['electronics'] . "%\n";

Output

GST (INR)
18%

Array constants can be read element by element, but — like all constants — never modified.


Visibility of Constants

class PaymentGateway
{
    public const CURRENCY = 'INR';        // part of the public API
    protected const RETRY_LIMIT = 3;      // for subclasses
    private const API_PATH = '/v2/pay';   // internal detail
}

Constants without a modifier are public. Making internal constants private keeps them out of the class's public API, so they can change freely (see PHP Access Modifiers).


Typed Class Constants (PHP 8.3+)

Constants can declare a type, which PHP checks — important when child classes or implementing classes redefine a constant:

<?php
interface HasVersion
{
    public const string VERSION = '1.0';
}

class ApiV2 implements HasVersion
{
    public const string VERSION = '2.0';   // must still be a string
}

echo ApiV2::VERSION . "\n";

Output

2.0

Redefining it with a value of a different type is a fatal error:

PHP Fatal error:  Cannot use int as value for class constant ApiV3::VERSION of type string

Without types, a subclass could accidentally change a constant from a string to an integer, breaking code that relies on string operations.


self:: vs static:: for Constants

self:: refers to the class where the code is written; static:: refers to the class of the object (or the class the method was called on). Child classes can redefine non-final constants, and static:: picks up the child's value:

<?php
class Notification
{
    public const CHANNEL = 'email';

    public function describe(): string
    {
        return 'self: ' . self::CHANNEL . ', static: ' . static::CHANNEL;
    }
}

class SmsNotification extends Notification
{
    public const CHANNEL = 'sms';
}

echo (new Notification())->describe() . "\n";
echo (new SmsNotification())->describe() . "\n";

Output

self: email, static: email
self: email, static: sms

Use static:: when subclasses are expected to customize a constant; use self:: when the value must be exactly the one defined in the current class. (This is late static binding, covered in Late Static Binding.) Mark constants final (PHP 8.1+) when subclasses must not change them (see The final Keyword).


Accessing Constants Dynamically

<?php
class Order
{
    public const STATUS_PENDING = 'pending';
    public const STATUS_PAID = 'paid';
}

$name = 'STATUS_PAID';

echo Order::{$name} . "\n";                  // PHP 8.3+
echo constant(Order::class . '::' . $name) . "\n";   // all versions

$statuses = array_filter(
    (new ReflectionClass(Order::class))->getConstants(),
    fn(string $constant): bool => str_starts_with($constant, 'STATUS_'),
    ARRAY_FILTER_USE_KEY
);
print_r($statuses);

Output

paid
paid
Array
(
    [STATUS_PENDING] => pending
    [STATUS_PAID] => paid
)
  • Order::{$name} (PHP 8.3+) and constant() fetch a constant whose name is in a variable.
  • Reflection lists all constants of a class — useful for building select lists or validation rules (see Reflection API).

Constants vs Enums

Groups of related constants such as order statuses are a classic use of class constants. Since PHP 8.1, enums are often the better tool for them:

Class ConstantsEnums
Values are plain strings/ints; any string is accepted where a status is expectedOrderStatus is a type; only valid cases are accepted
No built-in list of allowed valuesOrderStatus::cases() lists all values
Cannot have methods per valueEnum cases can have methods (label(), color())

Use class constants for configuration-like values (limits, rates, versions, keys) and enums for sets of states or options (see PHP Enums).


Practical Example: GST Calculation

<?php
final class GstCalculator
{
    private const RATES = [
        'books' => 0,
        'stationery' => 12,
        'electronics' => 18,
    ];

    public const DEFAULT_CATEGORY = 'electronics';

    public function tax(float $amount, string $category = self::DEFAULT_CATEGORY): float
    {
        if (!array_key_exists($category, self::RATES)) {
            throw new InvalidArgumentException("Unknown category: $category");
        }

        return round($amount * self::RATES[$category] / 100, 2);
    }
}

$gst = new GstCalculator();

$items = [
    ['Desk Lamp', 1299, 'electronics'],
    ['Notebook', 60, 'stationery'],
    ['PHP Handbook', 499, 'books'],
];

foreach ($items as [$name, $price, $category]) {
    printf("%-13s ₹%8.2f  GST ₹%6.2f\n", $name, $price, $gst->tax($price, $category));
}

Output

Desk Lamp     ₹ 1299.00  GST ₹233.82
Notebook      ₹   60.00  GST ₹  7.20
PHP Handbook  ₹  499.00  GST ₹  0.00

Step-by-Step Explanation

  1. The rate table is a private constant: an internal detail that callers cannot read or change.
  2. DEFAULT_CATEGORY is public, so callers can refer to the default, and it is also used as a parameter default (= self::DEFAULT_CATEGORY).
  3. Unknown categories are rejected, so a typo cannot silently produce a tax of zero.
  4. When rates change, only the constant changes; every calculation follows automatically.

(In a real application, tax rates that change over time are usually stored in the database with validity dates; constants suit values that change only with a code release.)


Common Mistakes

Using $ or -> With Constants

Effect: $this->MAX_ITEMS or Order::$MAX_ITEMS refer to properties, not constants.

Fix: Use self::MAX_ITEMS or Order::MAX_ITEMS.

Calling Functions in Constant Values

Effect: Compile error, because constants require constant expressions.

Fix: Use literals and operators, or initialize a property in the constructor.

Using self:: When Subclasses Override the Constant

Effect: The parent's value is used even in child objects.

Fix: Use static:: for overridable constants.

Using Constants Where an Enum Fits Better

Effect: Any string is accepted as a "status", and invalid values slip through.

Fix: Use enums for closed sets of values.


Best Practices

  • Replace magic numbers and strings with well-named class constants.
  • Keep constants in the class they relate to, and make internal ones private.
  • Add types to constants (PHP 8.3+) and mark non-overridable ones final.
  • Use static:: for constants that subclasses may customize.
  • Prefer enums for sets of related states or options.

Official reference: https://www.php.net/manual/en/language.oop5.constants.php

0 Comments

Reviewed before they appear

No comments yet.

PHP
PHP Home PHP Switch PHP Match Alternative Syntax PHP Loops While Loop Do While Loop For Loop Foreach Loop Nested Loops Break Statement PHP Intro PHP Install PHP Built-in Web Server PHP Syntax PHP Tags PHP and HTML PHP Comments PHP Multiline Comments PHP Variables Variables Scope PHP Casting Logical Operators Null Coalescing Operator Spaceship Operator Operator Precedence Continue Statement Modify Strings PHP If...Else...Elseif PHP If Modify Strings PHP If Operators PHP Functions Concatenate Strings PHP If...Else User-defined Functions Slicing Strings PHP Shorthand if Escape Characters Default Argument Values PHP Nested if Pass by Reference String Interpolation Named Arguments PHP File Upload Variadic Functions Return Values Search in Strings Multibyte Strings Type Declarations in Functions PHP Numbers PHP Math PHP Floats PHP Random Numbers PHP Constants PHP Properties PHP Methods PHP Constructor Constructor Property Promotion PHP Cookies PHP Destructor PHP Access Modifiers Readonly Properties PHP Sessions PHP Inheritance Method Overriding PHP Filters The final Keyword CSRF Protection PHP Filters Advanced Session Security Secure Cookies PHP Magic Constants File Upload Security PHP Callback Functions PHP Operators Filesystem Security PHP JSON Arithmetic Operators Error Reporting in Production Hiding PHP Assignment Operators PHP Configuration (php.ini) Comparison Operators PHP Command Line (CLI) PHP Exceptions Increment / Decrement Operators Migrating to PHP 8 PHP Form Validation PHP Form Required PHP Form URL/E-mail Error Handling Form Input Sanitizing Escaping Output (htmlspecialchars) Custom Error Handler Checkbox, Radio and Select Inputs Sticky Forms PHP Form Complete PHP Date and Time Date Formatting Error Logging HTTP Headers and Redirects Output Buffering Sending Email PHP cURL Consuming REST APIs PHP Generators AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll AJAX with JSON Security Introduction Password Hashing SQL Injection Prevention XSS Prevention Garbage Collection Project: Simple Blog PHP What is OOP PHP Classes/Objects The $this Keyword Access Array Items Update Array Items Add Array Items Remove Array Items Sorting Arrays Multidimensional Arrays Array Destructuring Array Spread Operator Search in Arrays Merge Arrays PHP Abstract Classes PHP Interfaces Polymorphism PHP Traits PHP Static Methods PHP Static Properties Late Static Binding Magic Methods Object Cloning MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where Debugging PHP Project: Image Upload Gallery $_SESSION $_ENV PHP RegEx RegEx Patterns Heredoc and Nowdoc RegEx Modifiers preg_match preg_replace preg_split PHP RegEx Functions Nullable Types Mixed and Never Types Nullsafe Operator PHP Attributes PHP Fibers Readonly Classes Property Hooks (PHP 8.4) Asymmetric Visibility (PHP 8.4) Reflection API SPL Data Structures Composer PHP Form Handling PDO Transactions PDO Error Handling PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser PHP XMLReader PHP XMLWriter AJAX Intro PHP Class Constants Encryption with Sodium
Ask about this post
AI Ask about this post

Ask questions about PHP Class Constants and get answers drawn from it.

Signed-in readers only.