Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

php - php7 void return type not working?

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 CodeBaseHttpRequester::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): CodeBaseHttpRequester::setResponseCode(500) #1 {main} thrown in /var/www/html/src/HttpRequester.php on line 86

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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, ajf@ajf.me
Status: Implemented (PHP 7.1)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...