Skip to content

Commit c78dc1f

Browse files
committed
Deprecate making __construct() and __destruct() a Generator
1 parent 404a9dc commit c78dc1f

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
Returning values from constructors and destructors is deprecated
3+
--FILE--
4+
<?php
5+
6+
class A {
7+
public function __construct() { return ''; }
8+
public function __destruct() { return ''; }
9+
}
10+
11+
class Gen {
12+
public function __construct() { yield ''; }
13+
public function __destruct() { yield ''; }
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Deprecated: Returning a value from a constructor is deprecated in %s on line %d
19+
20+
Deprecated: Returning a value from a destructor is deprecated in %s on line %d
21+
22+
Deprecated: Making a constructor a Generator is deprecated in %s on line %d
23+
24+
Deprecated: Making a destructor a Generator is deprecated in %s on line %d

Zend/zend_compile.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8853,6 +8853,14 @@ static zend_op_array *zend_compile_func_decl_ex(
88538853
zend_compile_params(params_ast, return_type_ast,
88548854
is_method && zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) ? IS_STRING : 0);
88558855
if (CG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
8856+
if (CG(active_class_entry) != NULL) {
8857+
if (zend_is_constructor(CG(active_op_array)->function_name)) {
8858+
zend_error(E_DEPRECATED, "Making a constructor a Generator is deprecated");
8859+
} else if (zend_string_equals_literal_ci(CG(active_op_array)->function_name, ZEND_DESTRUCTOR_FUNC_NAME)) {
8860+
zend_error(E_DEPRECATED, "Making a destructor a Generator is deprecated");
8861+
}
8862+
}
8863+
88568864
zend_mark_function_as_generator();
88578865
zend_emit_op(NULL, ZEND_GENERATOR_CREATE, NULL, NULL);
88588866
}

0 commit comments

Comments
 (0)