Mixed Pseudo Type in PHP 8

PHPServer Side ProgrammingProgramming

The mixed type in PHP 8 is a new built-in union type. Mixed type is equivalent to array|bool|callable|int|float. Mixing the type is not quite similar to omitting the type completely.

That means, the programmer just forgot to write it.

Sometimes the programmer prefers to omit some specific type to keep the compatibility with an older version.

Mixed type in PHP 8 can take any type of property/return/parameter. We can say that it includes null, callable, resource, all class objects, or all scalar types in PHP. The mixed type is equivalent to the Union type.


Example: Mixed Type in PHP 8

   class Student{
      public mixed $studentProperty;
      public function emp(mixed $emp): mixed {}
  • In PHP 8, Mixed is a pseudo/virtual type. It represents several types that PHP can handle, which means we cannot cast a variable to mixed because it does not make any logic.

$foo = (mixed) $bar;

Note: gettype() and get_debug_type() function can never return mixed as the type of a variable also.

  • We cannot use mixed in union with other types.

function(mixed|FooClass $bar): int|mixed {}

Note: In the above code, both union types are not allowed and It will give a fatal error.


Fatal error: Type mixed can only be used as a standalone type
in C:\xampp\htdocs\gud.php on line 2

Example: PHP 8 Program code using Mixed Type

   function debug_function(mixed ...$data){
   debug_function(10, 'string', []);


   [0] => 10
   [1] => string
   [2] => Array
Published on 01-Apr-2021 06:28:12