简体   繁体   English

PHP中的类型提示返回值/函数

[英]Type-hinting return value/functions in PHP

I did some research and came over this RFC which discusses type hinting for functions in PHP: https://wiki.php.net/rfc/returntypehint2 我进行了一些研究,并通过了RFC,该RFC讨论了PHP函数的类型提示: https//wiki.php.net/rfc/returntypehint2

For example 例如

public string getName()
{
    return 'martinmine';
}

Would be valid PHP code. 将是有效的PHP代码。 Returning for example an array would yield an error in this case. 在这种情况下,例如返回数组将产生错误。 Does anyone know the status on the RFC or if it got removed/added? 是否有人知道RFC上的状态,或者是否已删除/添加了RFC? I cannot seem to be able to make this function above work. 我似乎无法使此功能正常工作。 I am using PHP 5.5.9. 我正在使用PHP 5.5.9。

Type hinting for return types are postponed to PHP 7 (as of this article ), but the syntax is different than of the old purpose one: 有关返回类型的类型提示已推迟到PHP 7 (如本文所述 ),但其语法与原来的语法不同:

function foo(): array {
    return [];
}

But it is already available in PHP 5 for parameters of type class of a method or function(as of this article ): 但是它已经在PHP 5中用于方法或函数的类型类的参数(截至本文 ):

public function test_array(array $input_array) {
    print_r($input_array);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM