Skip to main content

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.

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.
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.
// 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.
// 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.
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.
class ParentClass {
    public function fetch(): mixed {
        return null;
    }
}

class ChildClass extends ParentClass {
    // Valid: Narrowing 'mixed' to 'array'
    public function fetch(): array {
        return [];
    }
}
Master PHP with Deep Grasping Methodology!Learn More