Skip to content

Commit ac479c0

Browse files
committed
Deprecate making __construct() and __destruct() a Generator
1 parent 0604999 commit ac479c0

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 B {
12+
public function __construct() { return $this->voidMethod(); }
13+
public function __destruct() { return $this->voidMethod(); }
14+
15+
public function voidMethod(): void { }
16+
}
17+
18+
class Gen {
19+
public function __construct() { yield ''; }
20+
public function __destruct() { yield ''; }
21+
}
22+
23+
?>
24+
--EXPECTF--
25+
Deprecated: Returning a value from a constructor is deprecated in %s on line %d
26+
27+
Deprecated: Returning a value from a destructor is deprecated in %s on line %d
28+
29+
Deprecated: Returning a value from a constructor is deprecated in %s on line %d
30+
31+
Deprecated: Returning a value from a destructor is deprecated in %s on line %d
32+
33+
Deprecated: Making a constructor a Generator is deprecated in %s on line %d
34+
35+
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_LCNAME) ? 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
}

ext/pdo_mysql/tests/pdo_mysql_subclass.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ $db = MySQLPDOTest::factory();
7676
$db->exec('DROP TABLE IF EXISTS test_subclass');
7777
?>
7878
--EXPECTF--
79+
Deprecated: Returning a value from a constructor is deprecated in %s on line %d
7980
__construct('%S', '%S', %s)
8081

8182
Deprecated: Callables of the form ["MyPDO", "parent::__construct"] are deprecated in %s on line %d

0 commit comments

Comments
 (0)