gpt4 book ai didi

PHP 混合类型 vs Typescript any

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:04:45 25 4
gpt4 key购买 nike

我在自定义函数中尝试使用 PHP 的 mixed 类型,但这个错误让我很困惑(标点符号是我的):

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

下面的一些(示例)代码会导致错误消息并说明我希望实现的目标。下面是一些 TLDR 和进一步的解释。但基本上,我将 mixed 视为某些 PHP 原生函数(例如 is_string 函数)的参数类型,并希望在自定义函数中执行相同的操作。

如何显式指定函数参数是多类型/混合/任意?

代码

<?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']);
?>

长见识

我是 PHP 的新手,但正在尝试“新的”显式类型系统。我使用的是 PHP 7.x,但我认为这是在 PHP 5.0 中引入的。我喜欢 TypeScript 语言的可选类型系统,最初假设 mixed 的工作方式与类型 any 相同。在 typescript 中。 PHP documentation on mixed只是加强了这个假设,因为它指出:

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

但在收到此错误后,似乎 mixed 是完全不同的东西。这是针对具有混合类型或其他值的数组吗?

最佳答案

要实现您想要的效果,您只需省略 mixed 并且不指定类型提示。 PHP NOT 没有语言关键字来明确指定参数可以是不同的类型。

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


PHP 代码中允许的类型提示

以下是允许的类型和使用它们的最低 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

mixed - PHP 8.0.0

static - PHP 8.0.0 (for return type only)

PHP 8.0 还引入了具有以下语法的联合类型:bool|string 这将允许 bool 值或字符串。 ( rfc )

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

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


PHPdoc 注释中允许的类型

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.

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

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

关键字(非 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 tobe 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 bindingin the PHP manual).

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

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

2021 年更新

mixed 类型已在 PHP8 中引入static 类型已在 PHP8 中引入

关于PHP 混合类型 vs Typescript any,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41267772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com