Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6464,8 +6464,16 @@ void zend_compile_func_decl(znode *result, zend_ast *ast, zend_bool toplevel) /*
zend_stack_push(&CG(loop_var_stack), (void *) &dummy_var);
}

zend_compile_params(params_ast, return_type_ast,
is_method && zend_string_equals_literal_ci(decl->name, "__toString") ? IS_STRING : 0);
uint32_t fallback_return_type = 0;
if (is_method) {
if (zend_string_equals_literal_ci(decl->name, "__toString")) {
fallback_return_type = IS_STRING;
} else if (zend_string_equals_literal_ci(decl->name, "__construct")) {
fallback_return_type = IS_VOID;
}
}
zend_compile_params(params_ast, return_type_ast, fallback_return_type);

if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
zend_mark_function_as_generator();
zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
Expand Down Expand Up @@ -6894,7 +6902,8 @@ void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /
zend_error_noreturn(E_COMPILE_ERROR, "Constructor %s::%s() cannot be static",
ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
}
if (ce->constructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
if (ce->constructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE
&& ZEND_TYPE_PURE_MASK(ce->constructor->common.arg_info[-1].type) != MAY_BE_VOID) {
zend_error_noreturn(E_COMPILE_ERROR,
"Constructor %s::%s() cannot declare a return type",
ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
Expand All @@ -6904,7 +6913,8 @@ void zend_compile_class_decl(znode *result, zend_ast *ast, zend_bool toplevel) /
if (ce->destructor->common.fn_flags & ZEND_ACC_STATIC) {
zend_error_noreturn(E_COMPILE_ERROR, "Destructor %s::%s() cannot be static",
ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
} else if (ce->destructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
} else if (ce->destructor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE
&& ZEND_TYPE_PURE_MASK(ce->destructor->common.arg_info[-1].type) != MAY_BE_VOID) {
zend_error_noreturn(E_COMPILE_ERROR,
"Destructor %s::%s() cannot declare a return type",
ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
Expand Down
17 changes: 17 additions & 0 deletions tests/classes/bug79679.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Bug #79679: Constructor can not return any value (always void).
--FILE--
<?php

class A {
public function __construct() {
return 5;
}
}

?>
--EXPECTF--
Fatal error: A void function must not return a value in %s:%d
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specific Constructor must not return a value message might be better

Stack trace:
#0 {main}
thrown in %s on line %d