TExcelTAD
Home › PHP › PHP Class Constants

PHP Class Constants

A class constant is a fixed value that belongs to a class instead of to individual objects. It is declared with the const keyword inside the class and never changes while the program runs. Class constants are great for names of states, limits, units and other "magic values" that would otherwise be scattered through the code.

Declaring and reading constants

Constant names are usually written in UPPER_SNAKE_CASE. You read them with the scope resolution operator :: — from outside using the class name, from inside using self:::

php · try it yourself
<?php
class Shipping {
    const FREE_LIMIT = 50;
    const RATE = 4.99;

    public function cost(float $orderTotal): float {
        return $orderTotal >= self::FREE_LIMIT ? 0 : self::RATE;
    }
}

$s = new Shipping();
echo "Free shipping from 
quot; . Shipping::FREE_LIMIT . "\n"; echo "Order 30: " . $s->cost(30) . "\n"; echo "Order 75: " . $s->cost(75) . "\n";
Free shipping from $50
Order 30: 4.99
Order 75: 0

No object is needed to read a constant: Shipping::FREE_LIMIT works on its own. You can also read it through an instance with $s::FREE_LIMIT.

Visibility and types

Class constants can have a visibility (since PHP 7.1) and, since PHP 8.3, a type:

php · try it yourself
<?php
class Http {
    public const int OK = 200;
    public const int NOT_FOUND = 404;
    private const array MESSAGES = [
        200 => 'OK',
        404 => 'Not Found',
    ];

    public static function message(int $code): string {
        return self::MESSAGES[$code] ?? 'Unknown';
    }
}

echo Http::OK . ' ' . Http::message(Http::OK) . "\n";
echo Http::NOT_FOUND . ' ' . Http::message(Http::NOT_FOUND) . "\n";
echo '500 ' . Http::message(500) . "\n";

try {
    echo Http::MESSAGES;
} catch (Error $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
200 OK
404 Not Found
500 Unknown
Error: Cannot access private constant Http::MESSAGES

Constants can hold scalars, arrays, enum cases and expressions built from other constants (const DAY = 24 * self::HOUR;).

self:: vs static::

When a child class redefines a constant, the choice of keyword matters:

  • self::NAME always means the constant of the class where the code is written.
  • static::NAME means the constant of the class the method was called on (late static binding).
php · try it yourself
<?php
class Model {
    const TABLE = 'models';

    public function selfTable(): string { return self::TABLE; }
    public function staticTable(): string { return static::TABLE; }
}

class User extends Model {
    const TABLE = 'users';
}

$u = new User();
echo "self::   " . $u->selfTable() . "\n";
echo "static:: " . $u->staticTable() . "\n";
echo "parent:  " . User::TABLE . " extends " . Model::TABLE . "\n";
self::   models
static:: users
parent:  users extends models

Use static:: when you want child classes to customise the value, as frameworks do for table names.

final constants

Since PHP 8.1 a constant can be marked final so child classes cannot redefine it. Interface constants can also be overridden by implementing classes unless they are final.

Special constant: ::class

Every class has a built-in ::class constant that returns its fully qualified name as a string. It is handy for logging, factories and avoiding typos:

php · try it yourself
<?php
class Report {}

$name = Report::class;
echo $name . "\n";

$obj = new $name();
echo get_class($obj) . "\n";
echo $obj::class . "\n";
Report
Report
Report

Constants vs other options

Feature Changes? Per object? Syntax
Class constant never no (shared) const MAX = 10; / self::MAX
Static property yes no (shared) static int $count; / self::$count
Normal property yes yes public int $x; / $this->x
Readonly property set once yes public readonly int $id;
Enum case never — Status::Active

Tip: If a constant represents one value from a fixed list (like STATUS_NEW, STATUS_PAID, STATUS_SHIPPED), consider an enum instead. Enums give you type safety that plain constants cannot.

Test yourself

How do you read the constant LIMIT from inside a method of the same class?

  1. $this->LIMIT
  2. self::LIMIT
  3. $LIMIT
  4. this::LIMIT

A parent method returns static::NAME. A child class redefines NAME. Calling the method on a child object returns…

  1. The parent's value
  2. The child's value
  3. An error
  4. null

What does Report::class evaluate to?

  1. A new Report object
  2. The string "Report" (fully qualified class name)
  3. true if the class exists
  4. The number of Report objects