我想知道为什么这是无效的:
Class Foobar
{
private function foo(): void {
return;
}
public function bar(): void {
return $this->foo(); // Compile Error: A void function must not return a value
}
}
我知道 RFC 是这样说的:
A function with a void return type, or void function, may either return implicitly, or have a return statement without a value
但是恕我直言 foo
是无效的,因此应该允许...
最佳答案
$this->foo()
不返回 void
,而是返回 null
。所以在你的 bar 函数中,你实际上明确地返回了 null
,这是不允许的。
关于php - void函数不能返回void函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40996004/