> ## 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 Final Class Constant

A final class constant in PHP is a constant declared with the `final` modifier within a class or interface, which explicitly prevents child classes or implementing classes from overriding its value. Introduced in PHP 8.1, this feature enforces strict immutability of the constant's value across the entire inheritance hierarchy.

Prior to PHP 8.1, class constants could be freely redefined by child classes. The `final` modifier alters this default behavior, ensuring the constant remains identical to its original definition throughout all derived contexts.

## Syntax

The `final` keyword is placed before or after the visibility modifier, preceding the `const` keyword.

```php theme={"dark"}
class Configuration {
    final public const MAX_CONNECTIONS = 100;
    protected final const TIMEOUT = 30;
}
```

## Inheritance Mechanics

When a class extends a parent class containing a `final` constant, any attempt to redeclare that constant in the child class will result in a compile-time `Fatal error`.

```php theme={"dark"}
class BaseClass {
    final public const ENVIRONMENT = 'production';
}

class ChildClass extends BaseClass {
    // Fatal error: ChildClass::ENVIRONMENT cannot override final constant BaseClass::ENVIRONMENT
    public const ENVIRONMENT = 'development'; 
}
```

## Visibility Constraints

The `final` modifier interacts specifically with visibility modifiers (`public`, `protected`, `private`):

* **Public and Protected:** The `final` modifier is fully supported and enforces the non-override rule in child classes.
* **Private:** The `final` modifier **cannot** be applied to `private` constants. Because `private` constants are restricted to the scope of the declaring class and are invisible to child classes, they inherently cannot be overridden. Attempting to declare a `final private const` will throw a `Fatal error`.

```php theme={"dark"}
class InvalidClass {
    // Fatal error: Private constant InvalidClass::SECRET cannot be declared final
    final private const SECRET = 'token'; 
}
```

## Interface Implementation

The `final` modifier can also be applied to constants within an `interface`. When a class implements an interface containing a final constant, the implementing class is prohibited from redefining it.

```php theme={"dark"}
interface DatabaseContract {
    final public const DEFAULT_PORT = 3306;
}

class MySQLAdapter implements DatabaseContract {
    // Fatal error: MySQLAdapter::DEFAULT_PORT cannot override final constant DatabaseContract::DEFAULT_PORT
    public const DEFAULT_PORT = 5432; 
}
```

*Note: In PHP, interface constants are implicitly public. If a visibility modifier is omitted in an interface, it defaults to `public`, and the `final` modifier will still operate as expected.*

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