> ## 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 Readonly Class

A `readonly` class in PHP (introduced in PHP 8.2) is a class modifier that implicitly declares all of its instance properties as `readonly`. Once an object of this class is instantiated and its properties are initialized, the object enforces shallow immutability. This prevents the reassignment of the properties themselves, though if a property holds a mutable object, the internal state of that referenced object can still be modified. Any attempt to reassign a property after initialization will throw an `Error` exception.

```php theme={"dark"}
readonly class Configuration
{
    public function __construct(
        public string $host,
        public int $port,
        public stdClass $metadata,
    ) {}
}

$config = new Configuration('localhost', 8080, new stdClass());

// Throws Error: Cannot modify readonly property Configuration::$port
// $config->port = 9000; 

// Valid: Shallow immutability allows modification of internal state of mutable objects
$config->metadata->environment = 'production'; 
```

## Core Mechanics and Constraints

Applying the `readonly` modifier to a class enforces a strict set of rules at compile-time and runtime:

**1. Implicit Property Modification**
Every declared instance property within the class automatically inherits the `readonly` modifier. You do not need to explicitly declare individual properties as `readonly`.

**2. Strict Typing Requirement**
All properties within a `readonly` class must have an explicit type declaration. Untyped properties will trigger a fatal error. If a property can accept any value, it must be explicitly typed as `mixed`.

**3. Static Properties Forbidden**
A `readonly` class cannot declare `static` properties. The `readonly` modifier applies exclusively to instance properties, and attempting to define a static property within a `readonly` class will result in a fatal compile-time error.

**4. Dynamic Properties Forbidden**
Dynamic properties are strictly prohibited on `readonly` classes. Additionally, a `readonly` class cannot be marked with the `#[AllowDynamicProperties]` attribute. Attempting to apply this attribute will trigger a fatal error.

**5. Inheritance Restrictions**
The `readonly` state dictates strict inheritance rules. A `readonly` class can only be extended by another `readonly` class. Conversely, a standard (non-readonly) class cannot be extended by a `readonly` class.

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