我有这样的功能:如何将null参数传递给接受:xhp作为参数的函数?
private static function myfun(
string $param1,
:xhp $param2,
): :xhp {
return
//somethinf
}
我不想传递任何事情param2的。我怎样才能做到这一点 ? 当我尝试像这样做:
myfun("Hi",null),
它显示我错误。
我有这样的功能:如何将null参数传递给接受:xhp作为参数的函数?
private static function myfun(
string $param1,
:xhp $param2,
): :xhp {
return
//somethinf
}
我不想传递任何事情param2的。我怎样才能做到这一点 ? 当我尝试像这样做:
myfun("Hi",null),
它显示我错误。
这是诀窍吗? private static function myfun( string $param1, :xhp $param2=null, ): :xhp { return //somethinf }
为了能够通过null
英寸,您的类型提示必须允许它。在Hack中,这是通过使用nullable type完成的。
private static function myfun(
string $param1,
?:xhp $param2,
): :xhp {
return
//somethinf
}
然后哈克类型检查还将确保您检查$param2
不使用它之前空。
不,它不工作:/ –