简体   繁体   English

php7 void 返回类型不起作用?

[英]php7 void return type not working?

I have a problem with return types in php7, specially "void".我在 php7 中的返回类型有问题,特别是“void”。

it works with all other types, int, string, null, bool, class objects.它适用于所有其他类型,int、string、null、bool、class 对象。

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.但是当我使用 void 时,它希望我返回一个对象 void 的实例,但实际上它不应该期望任何返回,因为这就是 void 的用途。

note: I'm running PHP 7.0.3注意:我正在运行 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未捕获的类型错误: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 中抛出

Void return types are for PHP 7.1 (which had not yet been released when you asked this). Void 返回类型适用于 PHP 7.1(在您询问时尚未发布)。 From the RFC来自 RFC

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

I've just found the answer here: https://wiki.php.net/rfc/void_return_type我刚刚在这里找到答案: https : //wiki.php.net/rfc/void_return_type

It will be a feature in PHP 7.1这将是 PHP 7.1 的一个特性

No there is not, until PHP 7.1.不,直到 PHP 7.1。 For PHP 7.0, you have to omit the return type completely for void functions/methods.对于 PHP 7.0,您必须完全省略void函数/方法的返回类型。

function printLn($a) {
    echo "$a\n";
}

Unfortunately, you then have no type safety for this function/method, and no TypeError will be thrown if you start returning something from it.不幸的是,你没有这个函数/方法的类型安全,如果你开始从它返回一些东西,也不会TypeError

Luckily, PHP 7.1 fixes this :幸运的是, PHP 7.1 解决了这个问题

Support for a new void return type is added.添加了对新的 void 返回类型的支持。 It requires that a function not return any value.它要求函数不返回任何值。

This is the correct syntax for PHP 7.1 :这是 PHP 7.1 的正确语法

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

This was postponed during the proposal that created return type hints :这在创建返回类型提示的提案中被推迟:

We keep the current type options .我们保留当前类型选项 Past proposals have suggested new types such as void, int, string or scalar;过去的提案建议使用新类型,例如 void、int、string 或 scalar; this RFC does not include any new types.此 RFC 不包含任何新类型。 Note that it does allow self and parent to be used as return types.请注意,它确实允许将 self 和 parent 用作返回类型。 ... ...

Future Work未来的工作

Ideas for future work which are out of the scope of this RFC include :超出本 RFC 范围的未来工作的想法包括

  • Allow functions to declare that they do not return anything at all (void in Java and C)允许函数声明它们根本不返回任何内容(Java 和 C 中的 void)

NULL also is not allowed as a return type. NULL也不允许作为返回类型。

tl;dr tl;博士

Return type void works since PHP 7.1 which is already available.返回类型 void 自 PHP 7.1 起就可用了。

Working syntax is:工作语法是:

<?php
function procedure(): void
{
//  return 'will not work';
}

echo procedure();

Why not just use 为什么不只是使用

   function printLn($a) {
    echo $a;
    return;}

It is the same as void. 它与void相同。

You can even remove the return with just the echo 您甚至可以只用回声删除返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM