|
| 1 | +--TEST-- |
| 2 | +Basic tests for explicit pass-by-ref |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +// Works (by-ref arg) |
| 7 | +$a = 42; |
| 8 | +incArgRef(&$a); |
| 9 | +var_dump($a); |
| 10 | + |
| 11 | +// Works (by-ref arg, deep reference) |
| 12 | +writeArgRef(&$b[0][1]); |
| 13 | +var_dump($b); |
| 14 | + |
| 15 | +// Works (prefer-ref arg) |
| 16 | +$c = 42; |
| 17 | +$vars = ['d' => &$c]; |
| 18 | +extract(&$vars, EXTR_REFS); |
| 19 | +$d++; |
| 20 | +var_dump($c); |
| 21 | + |
| 22 | +// Works (by-ref arg, by-ref function) |
| 23 | +$e = 42; |
| 24 | +incArgRef(&returnsRef($e)); |
| 25 | +var_dump($e); |
| 26 | + |
| 27 | +// Fails (by-val arg) |
| 28 | +try { |
| 29 | + $f = 1; |
| 30 | + var_dump(incArgVal(&$f)); |
| 31 | +} catch (Error $e) { |
| 32 | + echo $e->getMessage(), "\n"; |
| 33 | +} |
| 34 | + |
| 35 | +// Fails (by-ref arg, by-val function) |
| 36 | +try { |
| 37 | + $g = 42; |
| 38 | + incArgRef(&returnsVal($g)); |
| 39 | + var_dump($g); |
| 40 | +} catch (Error $e) { |
| 41 | + echo $e->getMessage(), "\n"; |
| 42 | +} |
| 43 | + |
| 44 | +// Fails (by-val arg, by-ref function) |
| 45 | +try { |
| 46 | + $h = 1; |
| 47 | + var_dump(incArgVal(&returnsRef($h))); |
| 48 | +} catch (Error $e) { |
| 49 | + echo $e->getMessage(), "\n"; |
| 50 | +} |
| 51 | + |
| 52 | +// Functions intentionally declared at the end of the file, |
| 53 | +// to avoid the fbc being known during compilation |
| 54 | +function incArgVal($a) { return $a + 1; } |
| 55 | +function incArgRef(&$a) { $a++; } |
| 56 | +function writeArgRef(&$a) { $a = 43; } |
| 57 | + |
| 58 | +function returnsVal($a) { return $a; } |
| 59 | +function &returnsRef(&$a) { return $a; } |
| 60 | + |
| 61 | +?> |
| 62 | +--EXPECT-- |
| 63 | +int(43) |
| 64 | +array(1) { |
| 65 | + [0]=> |
| 66 | + array(1) { |
| 67 | + [1]=> |
| 68 | + int(43) |
| 69 | + } |
| 70 | +} |
| 71 | +int(43) |
| 72 | +int(43) |
| 73 | +Cannot pass reference to by-value parameter 1 |
| 74 | +Cannot pass result of by-value function by reference |
| 75 | +Cannot pass reference to by-value parameter 1 |
0 commit comments