pro*_*sti 2 php standards php-internals php-7.1
我在这里找到了新的规范:https://wiki.php.net/rfc/void_return_type
function lacks_return(): void {
// valid
}
function returns_nothing(): void {
return; // valid
}
function returns_void(): void {
return void; // valid
}
Run Code Online (Sandbox Code Playgroud)
问:你知道幕后发生了什么吗?lacks_return函数实际上会返回void吗?
在幕后,PHP 检查函数中的return语句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;
}
Run Code Online (Sandbox Code Playgroud)
否则,void函数的编译就像普通一样.return没有值隐式返回NULL:
if (!expr_ast) {
expr_node.op_type = IS_CONST;
ZVAL_NULL(&expr_node.u.constant);
Run Code Online (Sandbox Code Playgroud)
并且每个函数都return在最后用隐式编译:
zend_emit_final_return(0);
Run Code Online (Sandbox Code Playgroud)
zn.op_type = IS_CONST;
if (return_one) {
ZVAL_LONG(&zn.u.constant, 1);
} else {
ZVAL_NULL(&zn.u.constant);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
379 次 |
| 最近记录: |