Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php7 void return type not working?

Tags:

I have a problem with return types in php7, specially "void".

it works with all other types, int, string, null, bool, class objects.

but when i use void it expecting me to return an instance of object void but in reality it should not expect any return as thats what void is for.

note: I'm running PHP 7.0.3

here is the code:

   public static function setResponseCode(int $code) : void     {         http_response_code($code);      } 

and error message is:

Uncaught TypeError: Return value of CodeBase\HttpRequester::setResponseCode() must be an instance of void, none returned in /var/www/html/src/HttpRequester.php:86 Stack trace: #0 /var/www/html/index.php(103): CodeBase\HttpRequester::setResponseCode(500) #1 {main} thrown in /var/www/html/src/HttpRequester.php on line 86

like image 503
nSams Dev Avatar asked Feb 18 '16 13:02

nSams Dev


People also ask

What is void return type in PHP?

A void return type has been introduced. Functions declared with void as their return type must either omit their return statement altogether, or use an empty return statement. null is not a valid return value for a void function.

What does void mean in PHP function?

Now in other programming languages when you specify a return type void it means you cannot return anything or you will get an error.


2 Answers

Void return types are for PHP 7.1 (which had not yet been released when you asked this). From the RFC

Version: 0.2.1
Date: 2015-02-14 (v0.1, later withdrawn), 2015-10-14 (v0.2, revival)
Author: Andrea Faulds, [email protected]
Status: Implemented (PHP 7.1)

like image 128
Machavity Avatar answered Oct 22 '22 21:10

Machavity


I've just found the answer here: https://wiki.php.net/rfc/void_return_type

It will be a feature in PHP 7.1

like image 36
rap-2-h Avatar answered Oct 22 '22 21:10

rap-2-h