Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A class constant is an immutable, statically-bound value defined within the scope of a class, interface, or trait. Unlike instance properties, class constants are allocated once per class rather than per object instantiation, and their values cannot be altered at runtime after initial compilation.

Declaration and Syntax

Class constants are defined using the const keyword. As of PHP 7.1, they support visibility modifiers (public, protected, private), with public being the default. As of PHP 8.3, class constants support explicit type declarations.
class Configuration {
    // Implicitly public, untyped constant
    const DEFAULT_TIMEOUT = 30;

    // Constant expression evaluation (PHP 5.6+)
    const CACHE_TTL = self::DEFAULT_TIMEOUT * 2;

    // Explicit visibility (PHP 7.1+)
    protected const MAX_RETRIES = 5;
    private const INTERNAL_FLAG = true;

    // Explicit visibility and type declaration (PHP 8.3+)
    public const string API_VERSION = 'v2.0';
}

Resolution and Access

Because constants are bound to the class structure itself, they are accessed using the Scope Resolution Operator (::, also known as Paamayim Nekudotayim), rather than the object operator (->).

Internal Access

Within the class hierarchy, constants are referenced using scope identifiers:
  • self::: Resolves to the class in which the code is explicitly written (Early Binding).
  • static::: Resolves to the class that was called at runtime, enabling Late Static Binding.
  • parent::: Resolves to the constant defined in the immediate parent class.
class Base {
    public const IDENTIFIER = 'BaseClass';
    
    public function getSelf(): string {
        return self::IDENTIFIER;
    }
    
    public function getStatic(): string {
        return static::IDENTIFIER;
    }
}

class Child extends Base {
    public const IDENTIFIER = 'ChildClass';
}

$instance = new Child();
echo $instance->getSelf();   // Outputs: BaseClass
echo $instance->getStatic(); // Outputs: ChildClass

External Access

From outside the class scope, constants are accessed using the fully qualified class name, a variable containing the class name, an object instance, or dynamically via a variable containing the constant name.
// Direct class access
echo Configuration::API_VERSION;

// Dynamic class name resolution
$className = 'Configuration';
echo $className::API_VERSION;

// Instance resolution
$config = new Configuration();
echo $config::API_VERSION;

// Dynamic class constant fetch (PHP 8.3+)
$constantName = 'API_VERSION';
echo Configuration::{$constantName};

The Magic ::class Constant

PHP automatically provides a magic class constant for all classes, interfaces, and traits. It resolves to the fully qualified class name (FQCN) as a string at compile time. As of PHP 8.0, ::class can also be evaluated on object instances to resolve their runtime class name.
namespace App\System;

class Router {}

// FQCN resolution
echo Router::class; // Outputs: App\System\Router

// Instance FQCN resolution (PHP 8.0+)
$router = new Router();
echo $router::class; // Outputs: App\System\Router

Technical Constraints

  1. Constant Expressions: The value assigned to a class constant must be a constant expression. It cannot be the result of a variable, a property, or a standard function call evaluated at runtime. It can, however, be a scalar expression (e.g., bitwise operations, mathematical operations) or an array.
  2. Interfaces and Traits: Interfaces can declare constants, which are inherited by implementing classes. Traits can also declare constants (as of PHP 8.2), which are flattened into the composing class during compilation.
  3. Overriding and Invariance: A child class can override a parent’s constant provided the visibility rules are respected (e.g., a protected constant can be overridden as public or protected, but not private). However, as of PHP 8.3, typed class constants are strictly invariant. When overriding a typed constant in a child class, the type must match the parent exactly; it cannot be widened or narrowed.
  4. Final Constants: As of PHP 8.1, constants can be declared final to strictly prevent overriding in child classes.
class ImmutableConfig {
    // Cannot be overridden by child classes (PHP 8.1+)
    final public const string VERSION = '1.0.0';
}

class ChildConfig extends ImmutableConfig {
    // Fatal error: Cannot override final constant
    // Fatal error: Type must be strictly invariant (if overriding were allowed)
    public const string VERSION = '2.0.0'; 
}
Master PHP with Deep Grasping Methodology!Learn More