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

The `mixed` type in PHP is a built-in, concrete type declaration introduced in PHP 8.0 that represents a predefined union of base types. It explicitly indicates that a variable, parameter, property, or return value can hold any type of data, while enforcing type declarations at the engine level.

Under the hood, `mixed` is strictly equivalent to the following union type:

`object|resource|array|string|float|int|bool|null`

## Syntax

The `mixed` type can be applied to class properties, function/method parameters, and return types.

```php theme={"dark"}
class DataContainer {
    public mixed $payload;

    public function process(mixed $input): mixed {
        $this->payload = $input;
        return $this->payload;
    }
}
```

## Structural Constraints

Because `mixed` is an all-encompassing union type, the PHP engine enforces strict rules regarding its declaration to prevent redundancy and logical conflicts.

**1. No Union Combinations**
You cannot combine `mixed` with any other type in a union declaration. Doing so results in a compile-time fatal error.

```php theme={"dark"}
// Fatal error: Type mixed can only be used as a standalone type
public function setIdentifier(mixed|string $id): void {}
```

**2. No Nullability Operator**
Because the `mixed` type inherently includes `null`, applying the nullable prefix (`?`) is redundant and triggers a fatal error.

```php theme={"dark"}
// Fatal error: mixed cannot be marked as nullable
public function getIdentifier(): ?mixed {
    return null;
}
```

**3. Exclusion of `void` and `never`**
The `mixed` type implies that a value *will* be evaluated or returned. It does not encompass `void` (which indicates no return value) or `never` (which indicates the function will not terminate normally). A function returning `mixed` must explicitly return a value, even if that value is `null`.

## Type Variance in Inheritance

When extending classes or implementing interfaces, `mixed` adheres to standard object-oriented variance rules (Liskov Substitution Principle).

**Parameter Contravariance**
A subclass can widen a specific parameter type from the parent class to `mixed`. This is valid because the subclass is accepting a broader range of inputs than the parent.

```php theme={"dark"}
class ParentClass {
    public function process(string $data): void {}
}

class ChildClass extends ParentClass {
    // Valid: Widening 'string' to 'mixed'
    public function process(mixed $data): void {}
}
```

**Return Covariance**
A subclass can narrow a `mixed` return type from the parent class to a specific, concrete type. This is valid because the subclass is guaranteeing a stricter output that still satisfies the parent's broad `mixed` contract.

```php theme={"dark"}
class ParentClass {
    public function fetch(): mixed {
        return null;
    }
}

class ChildClass extends ParentClass {
    // Valid: Narrowing 'mixed' to 'array'
    public function fetch(): array {
        return [];
    }
}
```

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