Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP 7.1 - Why no warning on void return value?

Tags:

php

php-7.1

The PHP manual notes this in its overview of the new void return type added in PHP 7.1:

Attempting to use a void function's return value simply evaluates to NULL, with no warnings emitted. 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"?

like image 917
Scott Buchanan Avatar asked Sep 14 '18 17:09

Scott Buchanan


2 Answers

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. 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:

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.

like image 130
NikiC Avatar answered Nov 07 '22 00:11

NikiC


A higher order function (HOF) is a function that follows at least one of the following conditions −

  • Takes one or more functions as arguments
  • Returns a function as its result

source

And then From the PHP Void RFC:

Since return; and return null; are technically equivalent in PHP; when a return value isn't specified, PHP will produce null for you. 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. Specifying it explicitly with 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!


To view exactly which functions would be called, try exploring the (open source) compile-time error handling logic of 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.

like image 7
Martin Avatar answered Nov 07 '22 00:11

Martin