Void as return type

61,003

Solution 1

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

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

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

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

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

Share:
61,003
Daan
Author by

Daan

Updated on July 11, 2022

Comments

  • Daan
    Daan almost 2 years

    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?

  • NikiC
    NikiC about 9 years
    There will be no void type in PHP 7. It might get introduced in 7.1.
  • mpen
    mpen almost 9 years
    @Siguza NikiC is a core PHP developer. If he says there will be no void type in PHP 7, I'd believe him.
  • BeNice
    BeNice over 8 years
    I have upticked this as a useful response BUT 100% disagree. I agree that void is not necessary but it is hugely desirable. The argument for void is straightforward - it does what is says, it is clear, it is readable. True you may be able to CONSTRUCT a void "return" but being able to say it explicitly would produce MUCH more comprehensible code (I always have trouble understanding my own code after 5 days away let alone other's). There's always such a push for brevity/reduced instruction set over readability. For me readability = maintainability which is a good trade off IMHO.
  • still_dreaming_1
    still_dreaming_1 almost 7 years
    It seems to work now, I think they added it to the language.
  • still_dreaming_1
    still_dreaming_1 almost 7 years
    Actually this is pretty cool as even the linter will now complain if you try to return something when the return type was declared void.