Pass by reference (5.1.1 vs 5.1.2)
| From: | Rishad Omar | Date: | Mon, 24 Jul 2006 15:52:38 +0000 |
| Subject: | Pass by reference (5.1.1 vs 5.1.2) | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-24932@lists.php.net to get a copy of this message | ||
Hello,
I've upgraded from 5.1.1 to 5.1.2 and discovered the following unexpected difference. Best shown by example.
<?
function getArray(&$arr)
{
$arr[] = 12;} getArray($p = array()); print_r($p); ?> In php 5.1.1, the $p is passed correctly as reference and so returns with the value 12 in its first element. I regard this as correct behaviour. In php 5.1.2, the $p is passed by value. So on return, $p is still empty! This can be fixed as follows: $p = array(); getArray($p); The following example works, as expected, in both 5.1.1 and 5.1.2. <? class A {
public $i = 9;} function doAmend(A &$a) // & not required because class instance passed by reference by default {
$a->i += 30;}
doAmend($b = new A);
print_r($b); // this prints the value of member i as 39 as expected.
?>