Say my function want to accept both string and integer. And if it is string, I convert it to int afterwards.
Like
function func(int|string $a) {
echo is_string($a)?intval($a)+1:$a+1;
}
func(1344);
func('1344');
The feature you asked for is in the proposal phase. See PHP RFC: Union Types .
Update
Mixed
type has been ultimately accepted and introduced in PHP 8 released in November 2020.
Mixed was accepted for PHP 8.0: https://wiki.php.net/rfc/mixed_type_v2
class B
{
public function foo(mixed $value) {}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.