FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » ArrayObject - copy or reference
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: ArrayObject - copy or reference [message #175173 is a reply to message #175171] Thu, 18 August 2011 18:44 Go to previous messageGo to previous message
Thomas Mlynarczyk is currently offline  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)
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Freelance PHP Developer
Next Topic: How to integrate paypal payment Indian currency support ..?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 28 20:25:31 GMT 2024

Total time taken to generate the page: 0.04621 seconds