Void as return type

PhpVoidReturn TypePhp 7

Php Problem Overview


I was testing return types with PHP 7.

I've created a simple script to test return types of PHP 7:

<?php

Class Obj {
    
    public function __construct(){
        
    }
    
    public function test(): string { //a string needs to be returned
        return "ok";
    }
    
}

function foo(): Obj { //instance of Obj needs to be returned
    return new Obj();
}

$o = foo();
echo $o->test(); // output: ok

Now in other programming languages when you specify a return type void it means you cannot return anything or you will get an error. So I wrote this script:

<?php

    function foo(): void {

    }

    foo(); 

Now in above script the expected output is nothing. Instead it gives me a Fatal error:

>Fatal error: Return value of foo() must be an instance of void, none returned on line 2

My question is (I couldn't find it), in PHP 7 will there be a similar void type?

Php Solutions


Solution 1 - Php

Edit:

A new separate RFC for a void return type has been published, has passed the vote, and was implemented in PHP 7.1.
There is now a void return type in PHP. :)

Original Post:

Taken from wiki.php.net:

> # Future Work > Ideas for future work which are out of the scope of this RFC include: > > * Allow functions to declare that they do not return anything at all (void in Java and C)

So currently there is no way to declare that you don't return anything.
I don't know what's best in your situation, but I'd probably just go with not declaring the return type for now.

To answer your question whether there will be a void return type in PHP 7:
There is no guarantee yet, but I think it is very likely that void or a synonym will be implemented in some way.

Solution 2 - Php

The voidreturn type was accepted for php 7.1. So it will come in the future.

Some examples on how it will work:

function should_return_nothing(): void {
    return 1; // Fatal error: A void function must not return a value
}

function returns_null(): void {
    return null; // Fatal error: A void function must not return a value
}
function lacks_return(): void {
    // valid
}
function returns_nothing(): void {
    return; // valid
}

See the RFC from Andrea Faulds for more info!

Solution 3 - Php

Edit: In PHP 7.1 there is a void pseudo-type. It is defined in the Void Return Type RFC. Below is the pre-7.1 answer.


Author of the return types RFC here. In PHP 7.0 there will not be void return types since the RFC didn't add it and neither did any other RFC targeting PHP 7.0.

The type void can exist in the PHP 7 series if we decide that adding new key/reserved words is okay for minor releases even though they will break code. This is somewhat uncertain, but it was done in PHP 5.4 with the callable keyword.


Personally, I don't think we need void; we already have null. From the manual:

> The special NULL value represents a variable with no value. NULL is the only possible value of type null.

In PHP a function which doesn't return anything will implicitly return null. This means that you cannot ever actually return nothing*. Going the null route means that there are no backwards compatibility breaks since null will not be a valid class/interface/trait name starting in PHP 7.0 and doesn't add any new key or reserved words.

*People familiar with the Zend Engine will realize that you can return nothing, but if you returned nothing the variable you are assigning will be assigned null, which makes them logically equivalent.

Solution 4 - Php

There is no equivalent type for void in php, return NULL; may fits your requirement since it doesn't have any type like 0 or any other value. Note: actual void means no return.

Solution 5 - Php

@BeNice I understand your point, anyhow I summarize the consideration from Levi Morrison as a practical question of sustainability: introducing void as a possible return type infact, we break the assumption that the only possible type of null is null.

This way, void should be returned for the type check of null, changing architecture constraints by design and causing a mess in backward compatibility.

// your choice implies this comparison should be true:
gettype(null) === void;

I think who used null not frequently in his code would bear the void type implementation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaanView Question on Stackoverflow
Solution 1 - PhpSiguzaView Answer on Stackoverflow
Solution 2 - PhpacrobatView Answer on Stackoverflow
Solution 3 - PhpLevi MorrisonView Answer on Stackoverflow
Solution 4 - PhpSanjay Kumar N SView Answer on Stackoverflow
Solution 5 - PhpyodabarView Answer on Stackoverflow