← Back to PHP Course | Chapter 8: OOP | Lesson 14 of 14

PHP Class Constants

A class constant is a fixed value that lives inside a class and is shared by everyone, like the number of sides on a triangle. It never changes and you read it through the class name.
Syntax
php
class ClassName {
    const CONSTANT_NAME = value;
}

echo ClassName::CONSTANT_NAME;   // outside the class
echo self::CONSTANT_NAME;        // inside the class

What Is a Class Constant?

A class constant is a named value defined with const directly inside a class body, shared by every instance and impossible to change after definition.

Unlike instance properties, a constant has no $ sigil and doesn't belong to any one object -- it belongs to the class itself, so its value is identical everywhere the class is used.

Example: What Is a Class Constant?

php
<?php
// Define the class `Circle`
class Circle {
    const PI = 3.14159;
}
// Print `Circle::PI` to the output
echo Circle::PI;
?>

Accessing Class Constants

You read a class constant with the scope resolution operator: ClassName::CONST_NAME from outside the class, or self::CONST_NAME from within its own methods.

Because constants aren't tied to an instance, you never need $this-> or an object variable to reach them.

Example: Accessing Class Constants

php
<?php
// Define the class `Circle`
class Circle {
    const PI = 3.14159;
    // Define the function `area` taking `$r`
    function area($r) {
        // Return `self::PI * $r * $r` from this function
        return self::PI * $r * $r;
    }
}
// Create a new `Circle` instance, stored in `$c`
$c = new Circle();
// Print `$c->area(2) . "\n"` to the output
echo $c->area(2) . "\n";
// Print `Circle::PI` to the output
echo Circle::PI;
?>

Constants vs Instance Properties

An instance property (private $rate) can hold a different value per object and can be reassigned; a class constant is fixed at definition time and shared identically across every instance.

Use a constant for a value that's inherently part of the class's definition -- like a fixed tax rate or a status code -- not something that varies per object.

Example: Constants vs Instance Properties

php
<?php
// Define the class `Invoice`
class Invoice {
    const TAX_RATE = 0.08;
    // Declare the private property `$amount`
    private $amount;
    // Define the constructor `__construct` taking `$amount`
    function __construct($amount) {
        $this->amount = $amount;
    }
}
// Print `Invoice::TAX_RATE` to the output
echo Invoice::TAX_RATE;
?>

Visibility on Constants

Since PHP 7.1, class constants can have visibility modifiers just like properties and methods: public const, protected const, or private const.

A private const is only reachable from inside the declaring class itself, letting you hide internal implementation constants from outside code.

Example: Visibility on Constants

php
<?php
// Define the class `Config`
class Config {
    public const VERSION = "1.0";
    private const SECRET_KEY = "abc123";
    // Define the function `getKey` with no parameters
    function getKey() {
        // Return `self::SECRET_KEY` from this function
        return self::SECRET_KEY;
    }
}
// Print `Config::VERSION . "\n"` to the output
echo Config::VERSION . "\n";
// Print `(new Config())->getKey()` to the output
echo (new Config())->getKey();
?>

Constants and Enums

PHP 8.1 introduced true enum types as a more structured alternative for a fixed set of related named values, with type-safety class constants don't have.

Class constants remain the right tool for a single fixed value (like a version string or a limit), while an enum fits better when you have several named, mutually-exclusive cases.

Example: Constants and Enums

php
<?php
class Status {
    const ACTIVE = 'active';
    const INACTIVE = 'inactive';
}
echo Status::ACTIVE;
// PHP 8.1 enums are a more structured alternative for fixed named cases
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to change a class constant at runtime, which is not allowed.
  2. Accessing a constant with -> instead of ClassName::CONSTANT.
  3. Forgetting that a private constant cannot be read from outside the class.
Chapter Summary
  • Classes and objects, constructors and destructors, and access modifiers form the core of PHP OOP.
  • Inheritance, method overriding, abstract classes, interfaces, and traits share and organize behavior.
  • Static members, magic methods, namespaces, the Iterator interface, and class constants add more features.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.