Skip to content

Commit 1f7167b

Browse files
committed
Deprecate returning values from __construct() and __destruct()
1 parent be41c36 commit 1f7167b

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

Zend/tests/prop_const_expr/non_enums_catchable.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const A_prop = (new A)->{new Printer ? 'printer' : null};
1818

1919
?>
2020
--EXPECTF--
21+
Deprecated: Returning a value from a constructor is deprecated in %s on line %d
2122
Printer
2223

2324
Fatal error: Uncaught Error: Fetching properties on non-enums in constant expressions is not allowed in %s:%d

Zend/tests/traits/bug60536_001.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ $a->__construct();
2323
echo "DONE";
2424
?>
2525
--EXPECTF--
26+
Deprecated: Returning a value from a constructor is deprecated in %s on line %d
27+
28+
Deprecated: Returning a value from a constructor is deprecated in %s on line %d
29+
2630
Warning: Undefined property: Z::$x in %s on line %d
2731
DONE

Zend/zend_compile.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,12 @@ ZEND_API bool zend_is_compiling(void) /* {{{ */
530530
}
531531
/* }}} */
532532

533+
static bool zend_is_constructor(const zend_string *name) /* {{{ */
534+
{
535+
return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
536+
}
537+
/* }}} */
538+
533539
static zend_always_inline uint32_t get_temporary_variable(void) /* {{{ */
534540
{
535541
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
55495555
}
55505556
/* }}} */
55515557

5552-
static bool zend_is_constructor(const zend_string *name) /* {{{ */
5553-
{
5554-
return zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME);
5555-
}
5556-
/* }}} */
5557-
55585558
static bool is_func_accessible(const zend_function *fbc)
55595559
{
55605560
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) /* {{{ */
59935993
zend_compile_expr(&expr_node, expr_ast);
59945994
}
59955995

5996+
if (expr_ast) {
5997+
if (CG(active_class_entry) != NULL) {
5998+
if (zend_is_constructor(CG(active_op_array)->function_name)) {
5999+
zend_error(E_DEPRECATED, "Returning a value from a constructor is deprecated");
6000+
} else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) {
6001+
zend_error(E_DEPRECATED, "Returning a value from a destructor is deprecated");
6002+
}
6003+
}
6004+
}
6005+
59966006
if ((CG(active_op_array)->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK)
59976007
&& (expr_node.op_type == IS_CV || (by_ref && expr_node.op_type == IS_VAR))
59986008
&& zend_has_finally()) {

0 commit comments

Comments
 (0)