Backend Development

PHP 8.0 New Pseudo-Type: The mixed Type

PHP 8.0 New Pseudo-Type The mixed Type

The mixed type is a special kind of type in PHP which indicates that the variable can be any type. Let’s see in this snippet how we can handle this in PHP 8.0.

 

 

 

Prior to PHP 8.0 the “mixed” type was an implicit type of any variable declared and can be assigned any value. Now in PHP 8.0 the variable can be declared explicitly as mixed using the “mixed” keyword.

mixed indicates to PHP that the property, parameter, return can can be any type.

To declare mixed type simply with the “mixed” keyword:

<?php

class Car {

    public mixed $wheels;


    public function setWheels(mixed $wheels) : mixed {
        $this->wheels = $wheels;

        return $wheels;
    }
}

Here i am declared a public mixed property $wheels. And also the setWheels() method accepts a parameter of type mixed in case you passed the wheels as an integer or string and returns a mixed type.

$car = new Car();

$car->wheels = 4;
$car->wheels = '4';

$car->setWheels('3');

echo $car->wheels;

 

As PHP 8.0 supports the union type also, the mixed type is equivalent to union type with all the types like so:

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

 

Using gettype() and get_debug_type() with mixed

The function gettype() and get_debug_type() returns the type of variable whether it’s int, string, bool, etc. When used with mixed it doesn’t return type as mixed because it’s pseudo-type instead it infers the actual data type of the variable.

In the previous code if you detect the type of the $wheels property:

echo get_debug_type($car->wheels);   // string

When there is no explicit type for a parameter or class property PHP assumes that the type is mixed.

For return types of functions also if no explicit type the type equal to mixed|void.

 

Using mixed with union type

Because mixed type represent all types, mixed can’t be used with unions of other types:

public function setWheels(mixed|string $wheels) : mixed {
    $this->wheels = $wheels;
        
        return $wheels;
}

PHP Fatal error:  Type mixed can only be used as a standalone type in index.php

Although the void type tells that the function return nothing, mixed can not be used with void in function return types.

public function setWheels(mixed $wheels) : mixed|void {
}

PHP Fatal error:  Type mixed can only be used as a standalone type

 

Using mixed with nullable

Mixed can’t be used with nullables because it includes the null type, so any attempt to do so it will throw error like above:

public function setWheels(mixed|null $wheels) : mixed {}
public function setWheels(?mixed $wheels) : mixed {}
public function setWheels(mixed $wheels) : mixed|null {}
public function setWheels(mixed $wheels) : ?mixed {}
PHP Fatal error:  Type mixed can only be used as a standalone type

 

Whether to use mixed or union

Always be more specific when declaring variables using the union type, for example if the function accepts a parameter that can be either integer or string it can be better declared as union int|string and there is no need to declare it as mixed. The mixed type is more suitable for functions that expected to accept any type, as an example a logging function.

 

0 0 votes
Article Rating

What's your reaction?

Excited
0
Happy
0
Not Sure
0
Confused
0

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments