Re: Pass by reference (5.1.1 vs 5.1.2)
| From: | Matt Sicker | Date: | Mon, 24 Jul 2006 16:12:30 +0000 |
| Subject: | Re: Pass by reference (5.1.1 vs 5.1.2) | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-24934@lists.php.net to get a copy of this message | ||
On Monday 24 July 2006 10:52, Rishad Omar wrote:
> 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.
>
> ?>
I'm guessing it has something to do with a memory leak. Either that, or
scope; creating a variable inside of a function call probably goes out
of scope after the function returns. Enable error_reporting(E_ALL|
E_STRICT) and see what happens.
--
Matt Sicker
Attachment: [application/pgp-signature]
Attachment: [application/pgp-signature]