PHP的混合类型与Typescript的任何
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP的混合类型与Typescript的任何,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3555字,纯文字阅读大概需要6分钟。
内容图文
![PHP的混合类型与Typescript的任何](/upload/InfoBanner/zyjiaocheng/750/a2c5fe8852694cbabff06095eab35b47.jpg)
我在自定义函数中尝试PHP的混合类型,但这个错误让我感到难过(标点符号是我的):
TypeError: Argument 1 passed to <functionName>() must be an instance of
mixed
,string
given.
下面的一些(示例)代码导致错误消息,并说明了我希望实现的目标.下面是一些TLDR的进一步解释.但basicall我看到混合作为一些PHP的本机函数(例如is_string函数)的参数类型,并希望在自定义函数中执行相同操作.
如何明确指定函数参数是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语言的可选输入系统,并且最初假设混合作品与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
警告:整数和布尔值不能用作类型提示
资料来源: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 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.
资源:
https://www.phpdoc.org/docs/latest/guides/types.html#primitives
内容总结
以上是互联网集市为您收集整理的PHP的混合类型与Typescript的任何全部内容,希望文章能够帮你解决PHP的混合类型与Typescript的任何所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。