在幕後,PHP checks for return
statements in void
functions,如果他們指定一個返回值,拋出一個編譯時錯誤:
/* `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
功能彙編作品像正常。 return
without a value implicitly returns NULL
:
if (!expr_ast) {
expr_node.op_type = IS_CONST;
ZVAL_NULL(&expr_node.u.constant);
而且每個功能is compiled with an implicit return
at the end:
zend_emit_final_return(0);
Whose return value is NULL
:
zn.op_type = IS_CONST;
if (return_one) {
ZVAL_LONG(&zn.u.constant, 1);
} else {
ZVAL_NULL(&zn.u.constant);
}
'這個函數返回也隱含空' - 是 –
NULL http://ideone.com?/AXErws或https://3v4l.org/RJRoR – 2016-11-14 01:04:10