简体   繁体   English

无效为返回类型

[英]Void as return type

I was testing return types with PHP 7. 我用PHP 7测试了返回类型。

I've created a simple script to test return types of PHP 7: 我创建了一个简单的脚本来测试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. 现在在其他编程语言中,当您指定返回类型为void ,意味着您无法返回任何内容,否则您将收到错误。 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 致命错误:foo()的返回值必须是void的实例,在第2行返回none

My question is (I couldn't find it), in PHP 7 will there be a similar void type? 我的问题是(我找不到),在PHP 7中会有类似的void类型吗?

Edit: 编辑:

A new separate RFC for a void return type has been published, has passed the vote, and was implemented in PHP 7.1. 已经发布了一个针对void返回类型的新单独RFC,已通过投票,并在PHP 7.1中实现。
There is now a void return type in PHP. PHP中现在有一个void返回类型。 :) :)

Original Post: 原帖:

Taken from wiki.php.net : 取自wiki.php.net

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)

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: 要回答您的问题,PHP 7中是否存在void返回类型:
There is no guarantee yet, but I think it is very likely that void or a synonym will be implemented in some way. 目前还不能保证 ,但我认为很可能会以某种方式实现void或同义词。

Author of the return types RFC here. 这里返回类型RFC的作者。 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. 在PHP 7.0中,不存在void返回类型,因为RFC没有添加它,也没有任何其他RFC针对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. 如果我们认为添加新的密钥/保留字对于次要版本是可以的,即使它们会破坏代码,PHP 7系列中也可以存在类型void This is somewhat uncertain, but it was done in PHP 5.4 with the callable keyword. 这有些不确定,但它是在PHP 5.4中使用callable关键字完成的。


Personally, I don't think we need void ; 就个人而言,我认为我们不需要void ; we already have null . 我们已经null From the manual : 从手册

The special NULL value represents a variable with no value. 特殊的NULL值表示没有值的变量。 NULL is the only possible value of type null. NULL是null类型唯一可能的值。

In PHP a function which doesn't return anything will implicitly return null . 在PHP中,不返回任何内容的函数将隐式返回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. 转到null路由意味着没有向后兼容性中断,因为null不是从PHP 7.0开始的有效类/接口/特征名称 ,并且不添加任何新的密钥或保留字。

*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. *熟悉Zend Engine的人会意识到你什么也不返回,但是如果你没有返回任何你分配的变量将被赋值为null,这使得它们在逻辑上是等价的。


In PHP 7.1 there will be a void pseudo-type. 在PHP 7.1中将有一个void伪类型。 It is defined in the Void Return Type RFC . 它在Void Return Type RFC中定义。

Personally I'm sad about this because the RFC author had previously "quit" and I had picked up the RFC. 我个人对此感到很难过,因为RFC作者此前已“退出”并且我已经选择了RFC。 Then next thing I know it's proposed and in discussion and she wouldn't wait for me to propose union types, which would have been the counterpart to void as noted above. 接下来我知道它的建议和讨论,她不会等我提出工会类型,如上所述,这将是无效的对应物。 Oh well. 那好吧。

The void return type was accepted for php 7.1. php 7.1接受了void返回类型。 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! 有关详细信息,请参阅Andrea FauldsRFC

There is no equivalent type for void in php, return NULL; php中没有等效的void类型,返回NULL; may fits your requirement since it doesn't have any type like 0 or any other value. 可能符合您的要求,因为它没有任何类型,如0或任何其他值。 Note: actual void means no return. 注意:实际的空白意味着没有回报。

@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 . @BeNice我明白你的意思,总之我总结从列维莫里森考虑作为可持续发展的一个现实的问题:引入void作为一个可能的返回类型逸岸,我们打破了假设,唯一可能的类型的nullnull

This way, void should be returned for the type check of null , changing architecture constraints by design and causing a mess in backward compatibility. 这样,应返回void以进行null类型检查,通过设计更改体系结构约束并导致向后兼容性混乱。

// 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. 我认为谁在他的代码中不经常使用null将承担void类型实现。

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

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