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
Switch to threaded view of this topic Create a new topic Submit Reply
ArrayObject - copy or reference [message #175050] Mon, 08 August 2011 17:28 Go to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
Please consider the following example code:

$array = array( 1, 2, 3 );
$obj = new ArrayObject( $array );
$obj[0] = 42;
echo $array[0];

With PHP 5.2.0 this prints 42, with PHP 5.2.4 however, I get 1 (as
expected). I have searched both on PHP's changelog [1] and Google, but
could not find any information about this issue. I assume it is a bug
that was fixed, but which one and in which version?

Greetings,
Thomas

[1] <http://php.net/ChangeLog-5.php>


--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: ArrayObject - copy or reference [message #175150 is a reply to message #175050] Tue, 16 August 2011 22:03 Go to previous messageGo to next message
Jo Schulze is currently offline  Jo Schulze
Messages: 15
Registered: January 2011
Karma: 0
Junior Member
Thomas Mlynarczyk wrote:

> Please consider the following example code:
>
> $array = array( 1, 2, 3 );
> $obj = new ArrayObject( $array );
> $obj[0] = 42;
> echo $array[0];
>
> With PHP 5.2.0 this prints 42, with PHP 5.2.4 however, I get 1 (as
> expected). I have searched both on PHP's changelog [1] and Google, but
> could not find any information about this issue. I assume it is a bug
> that was fixed, but which one and in which version?

With PHP5.3.7rc this prints 42 which I would expect. The line
$obj[0] = 42;
overwites the value with hash key 0

I see no bug here.
Re: ArrayObject - copy or reference [message #175161 is a reply to message #175150] Thu, 18 August 2011 11:48 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
Jo Schulze schrieb:

>> $array = array( 1, 2, 3 );
>> $obj = new ArrayObject( $array );
>> $obj[0] = 42;
>> echo $array[0];
>>
>> With PHP 5.2.0 this prints 42, with PHP 5.2.4 however, I get 1

> With PHP5.3.7rc this prints 42 which I would expect.

No it doesn't. I just installed PHP 5.3.7RC5 to test it. It prints 1.

> The line
> $obj[0] = 42;
> overwites the value with hash key 0

It overwrites the value with hash key 0 in *$obj*. But it should not
modify *$array*.

> I see no bug here.

There has definitely been a change in behaviour somewhere between 5.2.0
and 5.2.4. And that should be documented somewhere.

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: ArrayObject - copy or reference [message #175171 is a reply to message #175161] Thu, 18 August 2011 17:54 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/18/2011 7:48 AM, Thomas Mlynarczyk wrote:
> Jo Schulze schrieb:
>
>>> $array = array( 1, 2, 3 );
>>> $obj = new ArrayObject( $array );
>>> $obj[0] = 42;
>>> echo $array[0];
>>>
>>> With PHP 5.2.0 this prints 42, with PHP 5.2.4 however, I get 1
>
>> With PHP5.3.7rc this prints 42 which I would expect.
>
> No it doesn't. I just installed PHP 5.3.7RC5 to test it. It prints 1.
>
>> The line
>> $obj[0] = 42;
>> overwites the value with hash key 0
>
> It overwrites the value with hash key 0 in *$obj*. But it should not
> modify *$array*.
>
>> I see no bug here.
>
> There has definitely been a change in behaviour somewhere between 5.2.0
> and 5.2.4. And that should be documented somewhere.
>
> Greetings,
> Thomas
>

Thomas,

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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: ArrayObject - copy or reference [message #175173 is a reply to message #175171] Thu, 18 August 2011 18:44 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
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)
Re: ArrayObject - copy or reference [message #175175 is a reply to message #175173] Thu, 18 August 2011 23:26 Go to previous message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 8/18/2011 2:44 PM, Thomas Mlynarczyk wrote:
> 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
>
>

Yes, I think it's a bug in 5.2.0. But thinking more of it, I guess they
won't accept a bug against 5.2.0 any more.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Mon Nov 25 01:32:27 GMT 2024

Total time taken to generate the page: 0.02250 seconds