php typescript,PHP的混合类型与Typescript的任何

我在自定义函数中尝试PHP的混合类型,但这个错误让我感到难过(标点符号是我的):

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

下面的一些(示例)代码导致错误消息,并说明了我希望实现的目标.下面是一些TLDR的进一步解释.但basicall我看到混合作为一些PHP的本机函数(例如is_string函数)的参数类型,并希望在自定义函数中执行相同操作.

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

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'); //

echoMixed(['test1', 'test2']);

?>

TLDR

我是PHP的新手,但正在尝试’新’显式类型系统.我使用的是PHP 7.x,但我认为这是在PHP 5.0中引入的.我喜欢TypeScript语言的可选输入系统,并且最初假设混合作品与TypeScript中的类型any相同. PHP documentation on mixed只强化了这一假设,因为它表明:

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

但是在收到这个错误后,似乎混合是完全不同的东西.这是用于具有混合类型的值的数组吗?

解决方法:

要实现你想要的,你只需要省略混合而不指定typehint. PHP没有语言关键字来明确指定参数可以是不同的类型.

请注意,混合IS在文档中命名为关键字,但它不是“语言关键字”,而只是PHPDocs中的关键字.数组|对象,数字,void和其他伪类型也是如此.您可以在代码中使用的实际类型在PHP中命名为原语,请参阅以下文档摘录.

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

警告:整数和布尔值不能用作类型提示

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 more

information.

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 a

variable 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 to

be 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 binding

in the PHP manual).

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

标签:php,php-7,type-systems,typing

来源: https://codeday.me/bug/20190724/1523870.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值