Skip to main content
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.

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.
2. No Nullability Operator Because the mixed type inherently includes null, applying the nullable prefix (?) is redundant and triggers a fatal error.
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.
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.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More