可能会提示多个类型的提示吗? [英] Is it possible to type hint more than one type?

查看:86
本文介绍了可能会提示多个类型的提示吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用类型提示来允许两种不同的类型吗?

Can I allow two different types using type hinting?

例如参数$requester可以是UserFile:

function log (User|File $requester) {

}

推荐答案

学术上,这称为类型联合.

您可以通过创建接口,父类型等来作弊,如其他答案中所述,但是除了为您的项目增加复杂性和LoC之外,还有什么意义呢?另外,这不能用于标量类型,因为您不能扩展/实现标量类型.

You can cheat by creating interfaces, parent types, etc, as mentioned in other answers, but what's the point, apart for adding complexity and LoCs to your project? Plus, that can't work for scalar types as you can't extend/implement a scalar type.

您将得到相反的结果,而不是使代码更具可读性.除非那些类/接口已经存在并且由于OOP而在这里,否则不解决类型提示问题.

Instead of making the code more readable, you'll get the opposite. Except if those classes/interfaces already existed and they are here because of OOP, not to solve a type hinting problem.

PHP的规范答案是...好吧,只是不要输入类型提示.人们不认为该语言具有复杂而强大的类型系统,并且尝试解决该语言的缺陷并不是一个好的答案.

The canonical answer in PHP is... well, just don't put a type hint. The language was not thought to have a complex and powerful type system, and trying to workaround the flaws of the language is not a good answer.

相反,请正确记录功能:

Instead, document your function properly:

/**
 * Description of what the function does.
 *
 * @param User|File $multiTypeArgument Description of the argument.
 *
 * @return string[] Description of the function's return value.
 */
function myFunction($multiTypeArgument)
{

这至少会带来IDE对自动完成和静态代码分析的支持.在私人项目,网站等上工作足够好.

This will at least bring IDE support for autocompletion and static code analysis. Well enough when working on a private project, website, etc.

在设计公共API(PHP库等)时,有时您可能希望对API使用者的输入更具防御性.

When designing a public API (a PHP library, etc), sometimes you may want to be more defensive about API consumers' inputs.

然后@ tilz0R答案是必经之路:

Then @tilz0R answer is the way to go:

function log($message) {
    if (!is_string($message) && !$message instanceof Message) {
        throw new \InvalidArgumentException('$message must be a string or a Message object.');
    }

    // code ...
}

PHP(几乎)具有联合类型的那一天

2015年2月14日,针对PHP 7.1提出了联盟类型 PHP RFC.经过讨论和投票,结果被否决,18反对,11赞成.

The day PHP (almost) had union types

The 14th of February 2015, the Union Types PHP RFC was proposed for PHP 7.1. After discussion and vote, it's been rejected, 18 "no" against 11 "yes".

如果RFC被接受,则PHP的联合类型将与您显示的完全相同(User|File).

If the RFC had been accepted, PHP would have had union types exactly the way you've shown (User|File).

RFC有一些缺陷,但是被拒绝的主要原因是 mainteners 选民对改变的抵抗力很强,尤其是在关于类型严格性和其他编程范式(例如当默认值采用所有类型的值时,为什么我们需要类型联合" 不利于性能的结果" ).

The RFC had some flaws, but the main reason for why it's been rejected is that the mainteners voters are quite resistive to change especially when it's about type strictness and other programming paradigms (ex. "why would we need type unions when the default takes all types of values" and "that's not good for performance").

这篇关于可能会提示多个类型的提示吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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