PHP7的无效返回类型不起作用? [英] php7 void return type not working?

查看:246
本文介绍了PHP7的无效返回类型不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对php7中的返回类型(特别是"void")有疑问.

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

它可以与所有其他类型一起使用,例如int,string,null,bool,class对象.

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

但是当我使用void时,它期望我返回对象void的实例,但实际上,它不应期待任何返回,因为那就是void的作用.

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.

注意:我正在运行PHP 7.0.3

note: I'm running PHP 7.0.3

下面是代码:

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

    }

,错误消息是:

未捕获的TypeError:CodeBase \ HttpRequester :: setResponseCode()的返回值必须是void的实例,在/var/www/html/src/HttpRequester.php:86中未返回任何值:堆栈跟踪:#0/var/www /html/index.php(103):CodeBase \ HttpRequester :: setResponseCode(500)#1 {main}在第86行的/var/www/html/src/HttpRequester.php中抛出

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

推荐答案

无效的返回类型适用于PHP 7.1(在您询问时尚未发布). 来自RFC

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

版本:0.2.1
日期:2015-02-14(v0.1,后来撤回),2015-10-14(v0.2,复兴)
作者:Andrea Faulds,ajf @ ajf.me
状态:已实施(PHP 7.1)

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)

这篇关于PHP7的无效返回类型不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆