Skip to content

[RFC] Allow void return type on constructors/destructors #5717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
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
4 changes: 2 additions & 2 deletions Zend/tests/return_types/014.phpt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
--TEST--
Constructors cannot declare a return type
Constructors must return void
--FILE--
<?php

class Foo {
function __construct() : Foo {}
}
--EXPECTF--
Fatal error: Constructor %s::%s() cannot declare a return type in %s on line %d
Fatal error: Constructor %s::%s() must return void in %s on line %d
4 changes: 2 additions & 2 deletions Zend/tests/return_types/018.phpt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
--TEST--
Destructors cannot declare a return type
Destructors must return void
--FILE--
<?php

class Foo {
function __destruct() : Foo {}
}
--EXPECTF--
Fatal error: Destructor %s::%s() cannot declare a return type in %s on line %d
Fatal error: Destructor %s::%s() must return void in %s on line %d
10 changes: 6 additions & 4 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -6894,19 +6894,21 @@ 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",
"Constructor %s::%s() must return void",
ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
}
}
if (ce->destructor) {
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",
"Destructor %s::%s() must return void",
ZSTR_VAL(ce->name), ZSTR_VAL(ce->destructor->common.function_name));
}
}
Expand Down