是否可以为一个参数指定多个类型提示? [英] Is it possible to specify more than one type hint for a parameter?
本文介绍了是否可以为一个参数指定多个类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种方法可以向一个方法中添加多个类型提示?例如,foo(param)必须接收字符串OR bar OR baz的实例.
Is there a way to add more than one type hinting to a method? For example, foo(param) must receive a instance of string OR bar OR baz.
推荐答案
这是不可能强制执行的(方法内部除外).您只能提供单个类型提示,并且只能提供给对象/接口和数组(自PHP 5.1起).
That is not possible to enforce (except inside the method). You can only provide a single type hint, and only to objects/interfaces and arrays (since PHP 5.1).
您可以/应该将其记录在您的方法中,即:
You can/should however document it in your method, i.e:
/**
* @param string|Bar|Baz $param1
*/
function foo($param1);
这篇关于是否可以为一个参数指定多个类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文