Skip to content

PHP Tutorial Object-Oriented PHP

Class Constants

Lesson 208 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

A class constant is a value that belongs to a class and never changes. It is defined with const and read with ::.

index.php
PHP
<?php
class Order
{
    const STATUS_PENDING = "pending";
    const STATUS_PAID = "paid";
    const TAX_RATE = 0.18;
}

echo Order::STATUS_PAID . "\n";
echo Order::TAX_RATE;
Output
OUTPUT
paid
0.18

There is no $ and no new needed. Names are written in UPPER_CASE.

Why use them

Constants replace “magic values” scattered through your code. Compare:

index.php
PHP
if ($order->status === "paid") { ... }          // a typo "piad" is silent
if ($order->status === Order::STATUS_PAID) { ... }   // a typo is an error

Using them inside the class

index.php
PHP
<?php
class Cart
{
    const MAX_ITEMS = 3;

    private array $items = [];

    public function add(string $item): bool
    {
        if (count($this->items) >= self::MAX_ITEMS) {
            return false;
        }

        $this->items[] = $item;
        return true;
    }
}

$cart = new Cart();

foreach (["a", "b", "c", "d"] as $item) {
    echo $cart->add($item) ? "added " : "full ";
}
Output
OUTPUT
added added added full

Visibility

Constants can be public, protected or private. Public is the default.

index.php
PHP
<?php
class Secrets
{
    private const KEY = "internal";

    public static function reveal(): string
    {
        return self::KEY;
    }
}

echo Secrets::reveal() . "\n";

try {
    echo Secrets::KEY;
} catch (Error $e) {
    echo $e->getMessage();
}
Output
OUTPUT
internal
Cannot access private constant Secrets::KEY

Constants can use other constants

index.php
PHP
<?php
class Time
{
    const MINUTE = 60;
    const HOUR = self::MINUTE * 60;
    const DAY = self::HOUR * 24;
}

echo Time::DAY;
Output
OUTPUT
86400

final constants (PHP 8.1+)

A final constant cannot be overridden in a child class.

index.php
PHP
<?php
class Base
{
    final public const VERSION = "1.0";
}

Typed constants (PHP 8.3+)

From PHP 8.3 you can declare the type of a constant: const string STATUS_PAID = "paid";. On earlier versions this is a parse error.

index.php
PHP
<?php
class Order
{
    public const string STATUS_PAID = "paid";
    public const float TAX_RATE = 0.18;
}

static:: and inheritance

static::NAME looks the constant up in the class that was called, so a child class can change it.

index.php
PHP
<?php
class Shape
{
    const SIDES = 0;

    public function sides(): int
    {
        return static::SIDES;
    }
}

class Triangle extends Shape
{
    const SIDES = 3;
}

echo (new Triangle())->sides();
Output
OUTPUT
3

Constants vs enums

A group of related constants, such as order statuses, is often better as an enum (PHP 8.1+). An enum is its own type, so a function can accept only the allowed values. See Enums.

Reading all constants

index.php
PHP
<?php
class Color
{
    const RED = "#f00";
    const BLUE = "#00f";
}

print_r((new ReflectionClass("Color"))->getConstants());
Output
OUTPUT
Array
(
    [RED] => #f00
    [BLUE] => #00f
)
lightbulb
Prefer constants over repeated literals
If the same string or number appears in three places, give it a constant and a name.