简体   繁体   English

无效函数不能返回无效函数吗?

[英]Void functions can't return void functions?

I wonder why this is not valid: 我不知道为什么这是无效的:

Class Foobar
{
    private function foo(): void {
        return;
    }

    public function bar(): void {
        return $this->foo(); // Compile Error: A void function must not return a value
    }

}

I know the RFC says this: 我知道RFC这样说:

A function with a void return type, or void function, may either return implicitly, or have a return statement without a value 具有void返回类型的函数或void函数可以隐式返回,或者具有不带值的return语句

But IMHO foo is void, thus it should be allowed... 但是恕我直言foo是无效的,因此应允许...

$this->foo() does not return void , but null . $this->foo()不返回void ,但返回null So in your bar function, you are actually explicitly returning null , which is not allowed. 因此,在bar函数中,实际上是显式地返回null ,这是不允许的。

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

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