Re: ArrayObject - copy or reference [message #175173 is a reply to message #175171] |
Thu, 18 August 2011 18:44 |
Thomas Mlynarczyk
Messages: 131 Registered: September 2010
Karma:
|
Senior Member |
|
|
Jerry Stuckle schrieb:
> I get 1 here also (php 5.2.4). I would consider this a bug - you might
> want to post it to bugs.php.net.
But the bug was in 5.2.0 and seems now fixed in 5.2.4. Please look at my
code example again. I start with an array $array. Then I use that to
create an ArrayObject $obj, which should be a *copy* of $array, not a
reference to it. Now, whatever I do to $obj should not modify $array:
$array = array( 1, 2, 3 );
$obj = new ArrayObject( $array );
$obj[0] = 42;
echo $obj[0]; // This should print 42 and it does
echo '<br>';
echo $array[0]; // This should print 1 and it does in 5.2.4
From your and Jo's postings I get the impression that you both think
that $obj should be a reference to $array instead of an independent
copy. But the docs do not mention any passing by reference (like "&$input"):
ArrayObject::__construct() ([ mixed $input [, int $flags [, string
$iterator_class ]]] )
There is, however, a user comment addressing this issue and giving a
code example almost identical to mine:
<quote>Note that the first argument to ArrayObject::__construct, the
initial array, is passed by reference. ...</quote>
Considering the date of that comment, it certainly refers to the old
"pre-5.2.4" behaviour.
Do you think this change in behaviour was unintentional (and thus not
meant to be a bugfix) and nobody has noticed it so far and that's why I
cannot find any official documentation about it? Anyway, I think the new
behaviour is the "correct" one -- I would not expect $array to be passed
by reference when it's not explicitly documented. Also, this behaviour
makes more sense to me.
Greetings,
Thomas
--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
|
|
|