Mixed type PHP vs Typescript any

I tested the PHP mixed type in a user-defined function, but this error puzzled me (my punctuation):

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

Below is some (sample) code below, which causes an error message and illustrates what I was hoping to achieve. Below that some TL; DR with further explanation. But basicall I saw mixed as the parameter type of some native PHP functions (for example, is_string ) and wanted to do the same in a user-defined function.

How can I explicitly indicate that a function parameter is multi-type / mixed / any?

the 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($input); } } } echoMixed('test'); // <-- This results in the error. echoMixed(['test1', 'test2']); ?> 

TL; DR

I am new to PHP, but have tested a new explicit type system. I am using PHP 7.x, but I think it was introduced in PHP 5.0. I like the TypeScript optional input system, and the originally assumed mixed works just like the Type type in TypeScript. PHP's mixed documentation only reinforced this assumption as it reads:

mixed indicates that the parameter can take several (but not necessarily all) types.

But after receiving this error, it seems that mixed is something completely different. Is this for arrays with mixed type values โ€‹โ€‹or something else?

+3
php type-systems php-7 typing
source share
1 answer

To achieve what you want, you just need to leave mixed and not specify a type. PHP does NOT have a language keyword for explicitly specifying an argument; it can be of different types.

Note that mixed named keyword in the documentation, but it is not a "language keyword", but a keyword only in PHPDocs. The same applies to array|object , number , void and other pseudo-types. The actual types that you can use in your code are called primitives in PHP, see the following excerpts from the documentation.


Allowed Types in PHP Code

The following are valid types and the minimum version of PHP:

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

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

primitive php types

string A piece of text of indefinite length.

int or integer An integer number that can be positive or negative.

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

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

array A set of variables of an unknown type. You can specify the types of array elements, 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 zero. This type should not be confused with void, which is the complete absence of a variable or value (usually used with the @return tag).

called up . For a function or method that can be passed to a variable, see the PHP manual for more information on calling messages.

Keywords (not native to PHP)

mixed A value with this type can be literally anything; the documentation author cannot predict what type it will be.

void This is not the value you are looking for. The tag associated with this type does not intentionally return anything. Everything that is returned by a related element is random and not to be relied upon.

object Returns an object of any class

false or true Returns an explicit boolean; usually used when the method returns false or something in the result.

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

static The class object in which this value was consumed, if inherited, it will represent a child class. (see later static binding in the PHP manual).

$ this This is an exact instance of an object, commonly used to denote a free interface.

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

+6
source share

All Articles