PHP7.1中的隐式void返回?

PHP7.1中的隐式void返回?,php,standards,php-internals,php-7.1,Php,Standards,Php Internals,Php 7.1,我在这里找到了新规范: 问:你知道幕后发生了什么吗。缺少返回函数是否实际返回void?您自己可以很容易地进行测试: function lacks_return(): void { } function returns_nothing(): void { return; } echo gettype(lacks_return()); // NULL echo gettype(returns_nothing()); // NULL 因此答案是肯定的-有一个隐式的空(null)返回,因此

我在这里找到了新规范:


问:你知道幕后发生了什么吗。
缺少返回
函数是否实际返回
void

您自己可以很容易地进行测试:

function lacks_return(): void {
}

function returns_nothing(): void {
    return;
}

echo gettype(lacks_return()); // NULL
echo gettype(returns_nothing()); // NULL
因此答案是肯定的-有一个隐式的空(null)返回,因此您可以使用空返回或完全跳过它。哪种类型有意义-不返回任何内容与不返回任何内容相同?

在后台,PHP如果指定返回值,将抛出编译时错误:

/* `return ...;` is illegal in a void function (but `return;` isn't) */
if (return_info->type_hint == IS_VOID) {
    if (expr) {
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
            zend_error_noreturn(E_COMPILE_ERROR,
                "A void function must not return a value "
                "(did you mean \"return;\" instead of \"return null;\"?)");
        } else {
            zend_error_noreturn(E_COMPILE_ERROR, "A void function must not return a value");
        }
    }
    /* we don't need run-time check */
    return;
}
否则,
void
函数的编译工作正常:

每个功能:

:


<代码>此函数是否还返回隐式空值?< /代码> -YESULL或<代码> VARXDUP<<代码>将胜过<代码> ECHOR>代码>此示例请考虑新的空隙类型。这看起来像主菜。我真的在寻找一些调试,一些来自符号表或PHP AST解释的东西。但这也是很棒的bacuska。@RobbieAverill gettype()返回一个字符串,为什么var_dump()是一个字符串?:)抱歉-我以为这是
echo\u return()?我可能在做梦:)我最初接受了你的答案@DenisMysenko,我意识到改变这一点是不公平的,因为我经历了一段时间。因此,如果函数是
void
,编译时错误将防止不良做法返回non
void
?对吗?
/* `return ...;` is illegal in a void function (but `return;` isn't) */
if (return_info->type_hint == IS_VOID) {
    if (expr) {
        if (expr->op_type == IS_CONST && Z_TYPE(expr->u.constant) == IS_NULL) {
            zend_error_noreturn(E_COMPILE_ERROR,
                "A void function must not return a value "
                "(did you mean \"return;\" instead of \"return null;\"?)");
        } else {
            zend_error_noreturn(E_COMPILE_ERROR, "A void function must not return a value");
        }
    }
    /* we don't need run-time check */
    return;
}
if (!expr_ast) {
    expr_node.op_type = IS_CONST;
    ZVAL_NULL(&expr_node.u.constant);
zend_emit_final_return(0);
zn.op_type = IS_CONST;
if (return_one) {
    ZVAL_LONG(&zn.u.constant, 1);
} else {
    ZVAL_NULL(&zn.u.constant);
}