PHP的混合类型与Typescript的任何类型 [英] PHP's mixed type vs Typescript's any

查看:213
本文介绍了PHP的混合类型与Typescript的任何类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在自定义函数中尝试使用PHP的mixed类型,但是此错误使我难过(标点是我的):

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

TypeError:传递给< functionName>()的参数1必须是mixed的实例,给出了string.

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

下面的一些(示例)代码会导致错误消息并说明我希望实现的目标.下面是一些TLDR的进一步说明.但是我基本上将mixed视为PHP某些本机函数的参数类型(例如

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.

如何明确指定函数参数是multitype/mixed/any?

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

<?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

我对PHP还是很陌生,但是正在尝试新的"显式类型系统.我正在使用PHP 7.x,但我认为这是在PHP 5.0中引入的.我喜欢TypeScript语言的可选键入系统,最初假定mixed的工作方式与有关混合的PHP文档仅加强了这一假设,因为它指出: /p>

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 表示参数可以接受多种(但不一定是全部)类型.

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

但是在收到此错误后,似乎mixed完全不同.这是打算用于具有混合类型或其他值的数组吗?

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?

推荐答案

要实现所需的功能,您只需要省略mixed而不指定类型提示. PHP 没有使用language关键字来显式指定参数可以是不同的类型.

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.

请注意,mixed在文档中被命名为关键字,但它不是语言关键字",而是仅在PHPDocs中的关键字. array|objectnumbervoid和其他伪类型也是如此.您可以在代码中使用的实际类型在PHP中称为primitives,请参见以下文档摘录.

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.

以下是允许的类型以及使用它们的最低PHP版本:

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

类/接口名称- PHP 5.0.0

自我- PHP 5.0.0

数组- PHP 5.1.0

可调用- PHP 5.4.0

bool - PHP 7.0.0

浮动- PHP 7.0.0

int - PHP 7.0.0

字符串- PHP 7.0.0

警告:integerboolean不能用作类型提示

Warning: integer and boolean cannot be used as typehints

来源: http://php.net /manual/zh-CN/functions.arguments.php#functions.arguments.type-declaration

PHP基本类型

字符串,一段长度不确定的文本.

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.

布尔值或布尔值:只能包含状态"true"或"false"的变量.

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.

资源,如PHP手册中所述的文件处理程序或其他系统资源.

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

包含或返回的值实际上是空的.请勿将此类型与void混淆,后者是完全不存在 变量或值(通常与@return标记一起使用).

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).

可调用:可以通过变量传递的函数或方法,有关可调用对象的更多信息,请参见PHP手册.

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

关键字(非PHP原生)

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 返回任何类的对象,

false或true 返回一个明确的布尔值;通常在方法返回假"或某种后果的情况下使用.

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

self 使用该类型的类的对象,如果继承,它仍将代表最初定义该类的类.

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

静态使用该值的类的对象,如果继承,它将代表子类. (请参阅后期静态绑定 在PHP手册中).

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 This exact object instance, usually used to denote a fluent interface.

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

这篇关于PHP的混合类型与Typescript的任何类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆