Skip to content

Commit f0b190c

Browse files
committed
Deprecate return by ref from void function
Part of https://wiki.php.net/rfc/deprecations_php_8_1.
1 parent bf94010 commit f0b190c

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

UPGRADING

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ PHP 8.1 UPGRADE NOTES
330330
e.g. a truncation from 1.9 to 1, is deprecated. This affects array keys,
331331
int parameter and return types, and operators working on integers.
332332
RFC: https://wiki.php.net/rfc/implicit-float-int-deprecate
333-
. returning a non-array from __sleep will raise a warning
333+
. Returning a non-array from __sleep will raise a warning
334+
. Returning by reference from a void function is deprecated.
335+
RFC: https://wiki.php.net/rfc/deprecations_php_8_1
334336

335337
- Date:
336338
. The date_sunrise() and date_sunset() functions have been deprecated in
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Returning by reference from void function is deprecated
3+
--FILE--
4+
<?php
5+
6+
function &test(): void {
7+
}
8+
9+
$r =& test();
10+
var_dump($r);
11+
12+
?>
13+
--EXPECTF--
14+
Deprecated: Returning by reference from a void function is deprecated in %s on line %d
15+
16+
Notice: Only variable references should be returned by reference in %s on line %d
17+
NULL

Zend/zend_compile.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6532,6 +6532,11 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fall
65326532
}
65336533
arg_infos++;
65346534
op_array->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
6535+
6536+
if (ZEND_TYPE_CONTAINS_CODE(arg_infos[-1].type, IS_VOID)
6537+
&& (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
6538+
zend_error(E_DEPRECATED, "Returning by reference from a void function is deprecated");
6539+
}
65356540
} else {
65366541
if (list->children == 0) {
65376542
return;

0 commit comments

Comments
 (0)