简体   繁体   English

PHP 7.1 - 为什么void返回值没有警告?

[英]PHP 7.1 - Why no warning on void return value?

The PHP manual notes this in its overview of the new void return type added in PHP 7.1: PHP手册在PHP 7.1中添加的新void返回类型的概述中注意到了这一点:

Attempting to use a void function's return value simply evaluates to NULL, with no warnings emitted. 尝试使用void函数的返回值只需求值为NULL,不会发出警告。 The reason for this is because warnings would implicate the use of generic higher order functions. 这是因为警告会影响通用高阶函数的使用。

What does it mean by " warnings would implicate the use of higher order functions "? 警告会暗示使用高阶函数 ”是什么意思?

A higher order function (HOF) is a function that follows at least one of the following conditions − 高阶函数(HOF)是遵循以下条件中的至少一个的函数 -

  • Takes one or more functions as arguments 将一个或多个函数作为参数
  • Returns a function as its result 返回一个函数作为结果

source 资源

And then From the PHP Void RFC : 然后从PHP Void RFC

Since return ; 自从return ; and return null ; return null ; are technically equivalent in PHP; 在技​​术上等同于PHP; when a return value isn't specified, PHP will produce null for you. 如果未指定返回值,PHP将为您生成null However, choosing one over the other suggests intent . 但是,选择一个而不是另一个意味着意图 If you specify a value, it suggests the value is significant. 如果指定值,则表明该值很重要。 In a void function, the return value is insignificant: it's always the same and has no actual usefulness. 在void函数中,返回值是无关紧要的:它总是相同的,没有实际用处。 Specifying it explicitly with return null; 使用return null显式指定它; is pointless, because it doesn't really matter what value the function is going to return. 没有意义, 因为函数将返回什么值并不重要。

(My highlights) (我的亮点)

Therefore there's simply no need to provide a warning and it would simply require the usage of another function and additional significant compile-time overhead to notify a return error on a piece of code that is deliberately intended not to return . 因此,根本不需要提供警告,它只需要使用另一个函数和额外的重要编译时开销来通知故意不返回的代码片段的返回错误。

Think of it like this: 可以这样想:

  • I will keep lots of sponges on standby just incase the empty milk carton gets knocked over. 我会保持很多海绵待命,只是因为空的牛奶纸盒被撞倒了。

The carton will always be empty by intention so there's no need for going down the shop and buying 12 super absorbant sponges! 纸箱将永远是空的,所以没有必要去商店购买12个超级吸收海绵!


To view exactly which functions would be called, try exploring the (open source) compile-time error handling logic of PHP 7; 要准确查看将调用哪些函数,请尝试探索PHP 7的(开源)编译时错误处理逻辑; to see what functions will be called to process a function that causes a similar error (such as returning an unrecognised or incorrect type). 查看将调用哪些函数来处理导致类似错误的函数(例如返回无法识别或不正确的类型)。

These functions will be the ones that are not called by silently returning null instead of an error on PHP 7.1 intended void return types. 这些函数将通过静默返回null而不是PHP 7.1预期的void返回类型上的错误来调用

The problem are cases like these: 问题是这样的情况:

class Forwarder {
    public $obj; // Some object
    public function __call($method, $args) {
        return $this->obj->$method(...$args);
    }
}

class Obj {
    public function returnsVal(): int { return 42; }
    public function returnsVoid(): void { return; }
}

$fwd = new Forwarder;
$fwd->obj = new Obj;

// We want both of these calls to work
$val = $fwd->returnsVal();
$fwd->returnsVoid();

This code can handle both void and non-void functions. 此代码可以处理void和non-void函数。 If using the return value of a void function would warn, then we wouldn't be able to write this code and would have to do something like this instead: 如果使用void函数的返回值会发出警告,那么我们将无法编写此代码,而是必须执行以下操作:

class Forwarder {
    public $obj; // Some object
    public function __call($method, $args) {
        if (returns_void($this->obj, $method)) {
            $this->obj->$method(...$args);
        } else {
            return $this->obj->$method(...$args);
        }
    }
}

That's a lot of unnecessary boilerplate, not to mention that ''returns_void'' would have to be implemented using expensive reflection calls. 这是很多不必要的样板,更不用说''returns_void''必须使用昂贵的反射调用来实现。

As an addendum to Martin's answer, I believe that the following section from the Void RFC is also useful in clarifying the issue: 作为Martin答案的附录,我相信Void RFC的以下部分在澄清问题时也很有用:

Use of void functions in expressions 在表达式中使用void函数

In some other languages, such as C, a void function can't be used in an expression, only as a statement. 在某些其他语言(如C)中,void函数不能在表达式中使用,只能作为语句使用。 Since this RFC adds a way to specify a void function to PHP's syntax, it might be expected the same restriction would now apply in PHP. 由于这个RFC添加了一种为PHP的语法指定void函数的方法,因此可能预期现在同样的限制将适用于PHP。 However, this wouldn't match precedent. 但是,这与先例不符。 PHP has had 'void functions' of a kind since its inception, in the form of built-in functions, which are documented as “void” in the manual. PHP从一开始就具有“void函数”,以内置函数的形式,在手册中记录为“void”。 Such functions can be used in expressions, unlike in C. 这些函数可以在表达式中使用,与C不同。

We could change PHP's rules on void functions and disallow their use in expressions, but this would create a backwards-compatibility issue: it's not inconceivable that existing PHP code relies on being able to call built-in void functions in expressions, and plenty of code assumes that you can take the return value of an arbitrary PHP function (a callback, perhaps). 我们可以改变PHP关于void函数的规则并禁止它们在表达式中使用,但这会产生向后兼容性问题:现有的PHP代码依赖于能够在表达式中调用内置的void函数以及大量的代码并不是不可思议的。假设您可以获取任意PHP函数的返回值(可能是回调)。

Moreover, IDEs and other tools can warn the user when the return value of a void function is being used. 此外,IDE和其他工具可以在使用void函数的返回值时警告用户。 It isn't strictly necessary for the language itself to cover this. 语言本身并不一定要涵盖这一点。

https://wiki.php.net/rfc/void_return_type#use_of_void_functions_in_expressions https://wiki.php.net/rfc/void_return_type#use_of_void_functions_in_expressions

TL;DR TL; DR

PHP already had void builtins that were allowed to be used in expressions, and changing that now would be a big BC break. PHP已经有了允许在表达式中使用的void内置函数,并且现在更改它将是一个很大的BC中断。

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

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