PHP 中此类函数的名称是什么

What is the name of this type of function in PHP

我在探索一些laravel包代码时经常在类中看到这种类型的函数,我想知道这种类型的函数的名称是什么以及如何使用它,例如:

protected function getFiles(): Filesystem
{

}

protected function getConfigPath(): string
{

}

一份 PHP 文档 link 会很有用。

Return type declarations 添加到 PHP 7.

类似于参数类型声明,return类型声明 定义将由函数 return 编辑的值的 类型 。可用类型与 argument type declarations.

可用的类型相同
<?php
function sum($a, $b): float {
    return $a + $b;
}

As of PHP 7.1.0, return values can be marked as nullable by prefixing the type name with a question mark (?).

?string ?int ?array ?bool ?float 

这允许函数 return 定义的类型或 null,如果使用默认的 严格模式 ,任何其他内容都会抛出 TypeError。 =24=]

strict_types directive can be set globally, or toggled in code using the declare 结构。

declare(strict_types=1);

在 PHP 中使用这些功能的主要好处之一是代码更清晰。输入和输出函数没有歧义。它还有助于检测愚蠢的错误,例如比较字符串和整数类型的值 - 或者更确切地说,以更可预测的方式执行这些评估。

以下是关于此主题的一些其他讨论: