> ## 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 Nullable Property

A nullable property in PHP is a class property explicitly declared to accept `null` as a valid value in addition to its designated data type. Introduced alongside typed properties in PHP 7.4, nullability is enforced by the PHP engine at runtime, ensuring strict type adherence while allowing the absence of a value.

## Syntax

Nullability is denoted using either the short-hand question mark prefix (`?`) or, as of PHP 8.0, a Union Type explicitly including `null`.

```php theme={"dark"}
class Entity {
    // PHP 7.4+ syntax
    public ?string $shortHandNullable;
    
    // PHP 8.0+ Union Type syntax
    public string|null $unionNullable;
}
```

## Initialization and State Mechanics

A critical distinction in PHP's engine is that **nullable does not mean implicitly initialized to null**.

When a typed property (including a nullable one) is declared without a default value, it exists in an `uninitialized` state. Attempting to read an uninitialized property before assigning a value—even `null`—will throw a standard `Error`.

```php theme={"dark"}
class User {
    public ?string $uninitializedName;
    public ?string $initializedName = null;
}

$user = new User();

// Valid: Property was explicitly initialized to null
var_dump($user->initializedName); // Outputs: NULL

// Fatal Error: Typed property User::$uninitializedName must not be accessed before initialization
var_dump($user->uninitializedName); 
```

To resolve the uninitialized state, the property must be assigned a value (either of the declared type or `null`) via a default declaration, a constructor, or direct assignment prior to access.

## Inheritance and Type Variance

PHP enforces **invariant** property types during inheritance. This means a child class cannot alter the nullability of an inherited property. You cannot narrow a nullable property to a non-nullable property, nor can you widen a non-nullable property to a nullable one.

```php theme={"dark"}
class ParentClass {
    public ?string $identifier;
}

class ChildClass extends ParentClass {
    // Fatal error: Type of ChildClass::$identifier must be ?string (as in class ParentClass)
    public string $identifier; 
}
```

## Technical Constraints

1. **The `mixed` Type:** As of PHP 8.0, the `mixed` pseudo-type inherently includes `null`. Attempting to declare a property as `?mixed` or `mixed|null` will result in a compile-time fatal error due to redundancy.
2. **Syntax Redundancy:** You cannot combine the `?` prefix with a union type. Declaring `public ?string|int $value;` is invalid syntax. You must use `public string|int|null $value;`.
3. **Untyped Properties:** Properties declared without any type (e.g., `public $value;`) are implicitly nullable and default to `null` rather than an `uninitialized` state. The concept of a "nullable property" strictly applies to explicitly typed properties.

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