PHP's mixed type vs Typescript's any

9.8k views Asked by At

I was trying out PHP's mixed type in a custom function, but this error has me stumped (punctuation is mine):

TypeError: Argument 1 passed to <functionName>() must be an instance of mixed, string given.

Below some (sample) code below that causes the error message and illustrates what I was hoping to achieve. Below that some TLDR with further explanation. But basicall I saw mixed as the type of parameters of some of PHP's native functions (for example the is_string function) and wanted to do the same in a custom function.

How can I explicitly specify that a function parameter is multitype/mixed/any?

Code

<?php
    function echoMixed(mixed $input) {
        if (!is_array($input)) {
            echo mixed;
        } else {
            // For arrays echo each element using recursive call.
            foreach($input as $current) {
                echoMixed($current);
            }
        }
    }
    echoMixed('test'); // <-- This results in the error.
    echoMixed(['test1', 'test2']);
?>

TLDR

I'm pretty new to PHP, but was trying out the 'new' explicit type system. I'm using PHP 7.x but I think this was introduced in PHP 5.0. I love the system of optional typing of the TypeScript language, and initially assumed mixed works the same was as the type any in TypeScript. The PHP documentation on mixed only strengthened this assumption because it states:

mixed indicates that a parameter may accept multiple (but not necessarily all) types.

But after receiving this error it seems as though mixed is something completely different. Is this intended for arrays with values of mixed type or something?

1

There are 1 answers

4
ᴄʀᴏᴢᴇᴛ On

To achieve what you want you just have to leave out mixed and not specify a typehint. PHP does NOT have a language keyword to explicitly specify an argument can be different types.

Note that mixed IS named a keyword in the documentation, but it is not a 'language keyword', but a keyword only within the PHPDocs. The same goes for array|object, number, void, and the other pseudo-types. Actual types that you can use in code are named primitives in PHP, see the below excerpts from documentation.


Allowed typehints in PHP code

Here are the allowed types and the minimum PHP version to use them:

Class/interface name - PHP 5.0.0

self - PHP 5.0.0

array - PHP 5.1.0

callable - PHP 5.4.0

bool - PHP 7.0.0

float - PHP 7.0.0

int - PHP 7.0.0

string - PHP 7.0.0

mixed - PHP 8.0.0

static - PHP 8.0.0 (for return type only)

PHP 8.0 also introduces union types with the following syntax : bool|string which will allow booleans or strings. (rfc)

Warning: integer and boolean cannot be used as typehints

Source: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration


Allowed types in PHPdoc comments

PHP primitive types

string A piece of text of an unspecified length.

int or integer A whole number that may be either positive or negative.

float A real, or decimal, number that may be either positive or negative.

bool or boolean A variable that can only contain the state ‘true’ or ‘false’.

array A collection of variables of unknown type. It is possible to specify the types of array members, see the chapter on arrays for more information.

resource A file handler or other system resource as described in the PHP manual.

null The value contained, or returned, is literally null. This type is not to be confused with void, which is the total absence of a variable or value (usually used with the @return tag).

callable A function or method that can be passed by a variable, see the PHP manual for more information on callables.

Keywords (non native to PHP)

mixed A value with this type can be literally anything; the author of the documentation is unable to predict which type it will be.

void This is not the value that you are looking for. The tag associated with this type does not intentionally return anything. Anything returned by the associated element is incidental and not to be relied on.

object An object of any class is returned,

false or true An explicit boolean value is returned; usually used when a method returns ‘false’ or something of consequence.

self An object of the class where this type was used, if inherited it will still represent the class where it was originally defined.

static An object of the class where this value was consumed, if inherited it will represent the child class. (see late static binding in the PHP manual).

$this This exact object instance, usually used to denote a fluent interface.

Source: https://www.phpdoc.org/docs/latest/guides/types.html#primitives

Update 2021

mixed type has been introduced in PHP8 static type has been introduced in PHP8