24

是否可以像这样定义返回类型?

public static function bool Test($value)
{
      return $value; //this value will be bool
}
4

12 回答 12

63

由于这个问题仍然出现在搜索引擎结果中,这里有一个最新的答案:

PHP 7 实际上按照这个 RFC为函数/方法引入了正确的返回类型

这是上面链接的手册中的示例:

function sum($a, $b): float {
    return $a + $b;
}

或者,用更一般的表示法:

function function_name(): return_type {
    // some code
    return $var // Has to be of type `return_type`
}

如果返回的变量或值与返回类型不匹配,PHP 会将其隐式转换为该类型。或者,您可以通过 为文件启用严格类型declare(strict_types=1);,在这种情况下,类型不匹配将导致TypeError Exception

就这么简单。但是,请记住,您需要确保 PHP 7 在您的开发和生产服务器上都可用。

于 2016-08-16T09:08:47.687 回答
19

不可能在方法/函数级别显式定义返回类型。如指定的那样,您可以在返回中进行强制转换,例如...

return (bool)$value;

或者,您可以在 phpDoc 语法中添加注释,许多 IDE 将从类型完成的角度选择类型。

/**
 * example of basic @return usage
 * @return myObject
 */
function fred()
{
    return new myObject();
}
于 2011-02-24T12:26:10.010 回答
9

根据此页面: http: //php.net/manual/en/language.types.type-juggling.php

你可以试试做

return (boolean) $value;

现在 PHP 提供来自 PHP 7 的返回类型声明。我已经解释了如何在 PHP中使用返回类型

于 2011-02-24T12:23:45.547 回答
6

PHP 7 中添加的返回类型;)

https://wiki.php.net/rfc/return_types

于 2015-02-04T18:49:44.077 回答
4

自 PHP7 发布以来,他们现在向 PHP引入了返回类型声明。

: type在定义函数时通过附加来定义返回类型:

public function foo() : array
{
    return array();
}

默认弱模式

默认情况下,PHP 使用弱模式。该文档以下列方式解释它:

在默认的弱模式下,如果返回的值还不是正确的类型,它们将被强制转换为正确的类型

例如,请参阅以下示例,其中函数返回 a string,即使定义的返回类型是int

function myInt() : int {
    return "1";
}

echo gettype(myInt());
// Output is "integer"

强/严格模式

您还可以通过设置来告诉 PHP 在处理返回类型时进入严格/强模式declare(strict_types=1)。这意味着TypeError如果函数返回错误的类型,PHP 将抛出 a。PHP 文档是这样解释的:

在强模式下,返回的值必须是正确的类型,否则会抛出 TypeError。

declare(strict_types=1);
function myInt() : int {
    return "1";
}

echo gettype(myInt());
// Throws an TypeError
于 2017-03-29T04:17:36.857 回答
4

从 PHP 7.0 开始,您可以像这样声明返回类型:

function returnABool(): bool {
    return false;
}

不仅是常见的数据类型,您还可以返回预期的对象实例。例如,您希望一个函数总是返回一个类的实例。这可以这样实现:

class Biography {
    public function name() {
        return 'John Doe';
    }
}

现在你可以像这样声明这个对象的返回类型:

function get_bio(): Biography {
    return new Biography();
}

当函数或方法返回意外的数据类型时,会引发TypeError。这是一个您将遇到TypeError的示例。

class Biography {
    public function name() {
        return 'John Doe';
    }
}

class Bio2 {
    public function name() {
        return 'John Doe';
    }
}

function get_bio(): Biography {
    return new Bio2;
}

还有一个不会抛出任何TypeError的例子:

class Biography {
    public function name() {
        return 'John Doe';
    }
}

class Bio2 {
    public function name() {
        return new Biography();
    }
}

function get_bio(): Biography {
    $bio2 = new Bio2;
    return $bio2->name();
}

更多关于函数和方法声明返回数据类型的细节可以从官方文档中了解。

于 2019-11-27T19:25:56.680 回答
2

是的,您可以将其转换为bool.

public static function something($value) {
    return (bool)$value;
}

尽管从 PHP 7 开始,您可以显式强制返回类型:

public static function something($value) : bool {
    return $value;
}
于 2021-06-14T13:50:38.263 回答
1

PHP 7.0 引入了返回类型声明,所以你可以像下面这样使用:

public static function Test($value): bool  {
  return $value; //this value will be bool
}

有关详细信息,请访问官方文档。

https://www.php.net/manual/en/language.types.declarations.php

https://wiki.php.net/rfc/return_types

于 2021-06-11T22:26:21.657 回答
0

如果您知道返回值的类型将是 bool,那么在函数中强制转换它是没有意义的。

没有办法在 PHP 中定义函数的返回类型,因为它是松散类型的。您必须在调用函数中进行验证。

于 2011-02-24T12:26:46.513 回答
0

目前在 PHP 5 或更低版本中,您可以在函数符号中定义类型,但不能在返回值中定义。PHP 版本考虑了严格的声明。6及以上,但尚未实施。

也许通过一些技巧你可以做到这一点,比如在返回值之前进行转换......

于 2011-02-24T12:32:14.807 回答
0

确保覆盖不会更改返回类型的另一件事是执行以下操作:

public function Test($value)
{
      return (bool) OnTest();
}

private function OnTest($value)
{
      return $value;
}

通过这样做,您可以允许从您的类继承的类更改 OnTest 中的功能,但仍确保 Test 返回 bool 值。

于 2011-12-29T14:45:38.270 回答
-1

PHP 不允许你这样做,相反你可以这样做:

public static function Test($value)
{
      return (bool) $value; //this value will be bool
}

据我所知,您不能使用接口来强制返回类型

于 2011-02-24T12:24:37.437 回答