How to use mixed parameter type in my own functions?
Asked Answered
C

3

23

I want to define a PHP 7 function that takes a parameter of mixed type. (What I want is the equivalent of a generic type parameter in C#; if there's a better way to emulate that in PHP 7, please let me know.)

My code is as follows.

<?php
declare (strict_types = 1);    

function test (mixed $s) : mixed {
    return $s;
}

// Works
echo gettype ('hello');
// Does not work
echo test ('hello');
?>

When I run this code, I get the following.

Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of mixed, string given, called in mixed.php on line 11 and defined in mixed.php:4
Stack trace:
#0 mixed.php(11): test('hello')
#1 {main}
thrown in mixed.php on line 4

If I comment out the call to test(), the code runs fine, so apparently it's at least okay for me to use the mixed parameter type in the function declaration.

I know built-in PHP functions such as gettype() can take mixed parameters, though I don't know if they are internally using strict typing.

I see that "mixed" is also used as a pseudo-type in PHP documentation, so I might misunderstand the purpose of "mixed" as a PHP keyword, but what I'm seeing here at least implies to me that it is a legitimate keyword. Am I simply using it in a way it isn't intended for?

Finally, I realize I could circumvent all this by simply not specifying the parameter type, but I'd like to be consistent by specifying all my parameter and return types.

Thank you and please let me know if I can provide any additional information.

Carditis answered 13/5, 2016 at 0:18 Comment(8)
Yes, mixed is not a real type. And being consistent is orthogonal to proper semantics here.Grindelia
FYI mixed is not a Type. (php.net/manual/en/language.pseudo-types.php) It is only a pseudo type: a Hint that any Type might be passed to or returned from a Method... or perhaps for a variable which was loosely typed as mixed for any reason... Unlike in C#, what you are trying to achieve might be tricky in PHP especially with strict_types set to true. However, you can achieve a nearly similar effect without Strict Typing in which case your Methods may accept any Type so long as you don't supply any Type Hints. Though For C# Programmers that's bad yet that is the juice of PHPAbbevillian
Thank you very much mario and Poiz. Looks like I'll just settle for adding /*mixed*/ as a comment.Carditis
@Poiz, you should make your comment an actual answer. Then FsharpNoob could mark it as accepted. I just asked the same question. For me the answer it is only a 'pseudo-type' is not completely clear (at least a priori). For instance in CSS pseudo-elements, aren't actual elements, but are still language constructs / keywords. So I - wrongly - assumed pseudo meant a variable could never have this type, but that you could still define an argument as mixed (similar to an abstract class which you can't instantiate).Cede
@Cede --- Thanks a lot for the suggestion... Will do that immediately....Abbevillian
Bart, @Poiz, thank you, accepted.Carditis
At least, you can add @param mixed $s to document your method, then phpStorm will not complain about the missing typeGraphics
@Abbevillian (not that you can edit your comment anymore, but https://www.php.net/manual/en/language.pseudo-types.php is a dead link.Shue
A
29

FYI mixed is not a Type. (Refer to the Documentation). It is only a pseudo type: a Hint that any Type might be passed to or returned from a Method... or perhaps for a variable which was loosely typed as mixed for any reason...

Unlike in C#, what you are trying to achieve might be tricky in PHP especially with strict_types set to true.

However, you can achieve a nearly similar effect without Strict Typing - in which case your Methods may accept any Type so long as you don't supply any Type Hints. Though For C# Programmers, that's bad - yet, that is the juice of PHP.

Abbevillian answered 22/12, 2016 at 6:40 Comment(0)
B
15

UPDATE 2021

Mixed type has been introduced in PHP8. It can be used exactly as shown in the question.

Bedplate answered 12/1, 2021 at 10:26 Comment(1)
Link to PHP documentation about the mixed type: php.net/manual/en/…Eunaeunice
B
1

To indicate that the function also accepts null the ?-operator works for this also as mentioned on http://php.net/manual/de/functions.returning-values.php#functions.returning-values.type-declaration

But this is only usable if the code hasn't to be backward compatible because this feature is only available on PHP >= 7.1 As you can see on https://wiki.php.net/rfc/mixed-typehint there as RFC for adding mixed for type hinting. So actually there seem to be no chance to defined a correct type hint for parameters that accept more the one type.

So a phpDoc comment could be a solution (and it's also backward compatible). Example:

/**
* Functiondescription
* @author FSharpN00b
* @param mixed $s
* @return mixed
**/
function test ($s){
    return $s;
}
Billion answered 31/1, 2018 at 8:27 Comment(1)
@FrankerZ I've updated it. Hope now it is more helpful.Billion

© 2022 - 2024 — McMap. All rights reserved.