> ## 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.

# PHP Class Constant

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.

```php theme={"dark"}
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.

```php theme={"dark"}
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.

```php theme={"dark"}
// 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.

```php theme={"dark"}
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.

```php theme={"dark"}
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'; 
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
