From 1f7167b1af39d6302a2186933afc79ab8b1e4fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 8 May 2026 10:16:40 +0200 Subject: [PATCH 1/2] Deprecate returning values from `__construct()` and `__destruct()` --- .../prop_const_expr/non_enums_catchable.phpt | 1 + Zend/tests/traits/bug60536_001.phpt | 4 ++++ Zend/zend_compile.c | 22 ++++++++++++++----- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Zend/tests/prop_const_expr/non_enums_catchable.phpt b/Zend/tests/prop_const_expr/non_enums_catchable.phpt index 6f410ac7acbe..702bc024122d 100644 --- a/Zend/tests/prop_const_expr/non_enums_catchable.phpt +++ b/Zend/tests/prop_const_expr/non_enums_catchable.phpt @@ -18,6 +18,7 @@ const A_prop = (new A)->{new Printer ? 'printer' : null}; ?> --EXPECTF-- +Deprecated: Returning a value from a constructor is deprecated in %s on line %d Printer Fatal error: Uncaught Error: Fetching properties on non-enums in constant expressions is not allowed in %s:%d diff --git a/Zend/tests/traits/bug60536_001.phpt b/Zend/tests/traits/bug60536_001.phpt index a58098ca3112..712600a4b1d1 100644 --- a/Zend/tests/traits/bug60536_001.phpt +++ b/Zend/tests/traits/bug60536_001.phpt @@ -23,5 +23,9 @@ $a->__construct(); echo "DONE"; ?> --EXPECTF-- +Deprecated: Returning a value from a constructor is deprecated in %s on line %d + +Deprecated: Returning a value from a constructor is deprecated in %s on line %d + Warning: Undefined property: Z::$x in %s on line %d DONE diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 105f99d24171..3f81931dcb0f 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -530,6 +530,12 @@ ZEND_API bool zend_is_compiling(void) /* {{{ */ } /* }}} */ +static bool zend_is_constructor(const zend_string *name) /* {{{ */ +{ + return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME); +} +/* }}} */ + static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */ { return (uint32_t)CG(active_op_array)->T++; @@ -5549,12 +5555,6 @@ static void zend_compile_method_call(znode *result, zend_ast *ast, uint32_t type } /* }}} */ -static bool zend_is_constructor(const zend_string *name) /* {{{ */ -{ - return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME); -} -/* }}} */ - static bool is_func_accessible(const zend_function *fbc) { if ((fbc->common.fn_flags & ZEND_ACC_PUBLIC) || fbc->common.scope == CG(active_class_entry)) { @@ -5993,6 +5993,16 @@ static void zend_compile_return(const zend_ast *ast) /* {{{ */ zend_compile_expr(&expr_node, expr_ast); } + if (expr_ast) { + if (CG(active_class_entry) != NULL) { + if (zend_is_constructor(CG(active_op_array)->function_name)) { + zend_error(E_DEPRECATED, "Returning a value from a constructor is deprecated"); + } else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) { + zend_error(E_DEPRECATED, "Returning a value from a destructor is deprecated"); + } + } + } + if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) && (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR)) && zend_has_finally()) { From e8fa7103653c2a7dcd82d18a95125f59757dbebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Fri, 29 May 2026 15:48:22 +0200 Subject: [PATCH 2/2] Deprecate making `__construct()` and `__destruct()` a `Generator` --- .../constructor_destructor_return.phpt | 24 +++++++++++++++++++ Zend/zend_compile.c | 8 +++++++ 2 files changed, 32 insertions(+) create mode 100644 Zend/tests/magic_methods/constructor_destructor_return.phpt diff --git a/Zend/tests/magic_methods/constructor_destructor_return.phpt b/Zend/tests/magic_methods/constructor_destructor_return.phpt new file mode 100644 index 000000000000..5befa60760b9 --- /dev/null +++ b/Zend/tests/magic_methods/constructor_destructor_return.phpt @@ -0,0 +1,24 @@ +--TEST-- +Returning values from constructors and destructors is deprecated +--FILE-- + +--EXPECTF-- +Deprecated: Returning a value from a constructor is deprecated in %s on line %d + +Deprecated: Returning a value from a destructor is deprecated in %s on line %d + +Deprecated: Making a constructor a Generator is deprecated in %s on line %d + +Deprecated: Making a destructor a Generator is deprecated in %s on line %d diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 3f81931dcb0f..a1e488ba5e7f 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -8853,6 +8853,14 @@ static zend_op_array *zend_compile_func_decl_ex( zend_compile_params(params_ast, return_type_ast, is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0); if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) { + if (CG(active_class_entry) != NULL) { + if (zend_is_constructor(CG(active_op_array)->function_name)) { + zend_error(E_DEPRECATED, "Making a constructor a Generator is deprecated"); + } else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) { + zend_error(E_DEPRECATED, "Making a destructor a Generator is deprecated"); + } + } + zend_mark_function_as_generator(); zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL); }