|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Howdy - found an interesting one here. I am trying to create an array of
objects. I've got some well formed classes and in some cases all i need to do is to create an array of a particular class. after a few hours of pulling the hair out i think i have found a bug in the windows version of php (after i have tested it against a linux installation): here is the code: <? class t1 { var $num; } $x = new t1(); for ($c=0;$c<5;$c++) { $x->num = $c; $Array[] = $x; } echo "<pre>"; print_r( $Array ); echo "</pre>"; ?> LINUX OUTPUT (as expected): Array ( [0] => t1 Object ( [num] => 0 ) [1] => t1 Object ( [num] => 1 ) [2] => t1 Object ( [num] => 2 ) [3] => t1 Object ( [num] => 3 ) [4] => t1 Object ( [num] => 4 ) ) WINDOWS OUTPUT (WTF?!): Array ( [0] => t1 Object ( [num] => 4 ) [1] => t1 Object ( [num] => 4 ) [2] => t1 Object ( [num] => 4 ) [3] => t1 Object ( [num] => 4 ) [4] => t1 Object ( [num] => 4 ) ) So as you can see, each object in the windows array has been replaced with the last object that was assigned to the array. If i display the array as it is being built on each iteration of the loop, you can see quite clearly that the previously added objects are being overwritten with the last-added object. Thus you end up with an array with the correct number of objects, but each object is simply a copy of the last object added to the array. In this test example, if i add the 'num' value to the array instead (ie. $Array = $x->num), it works perfectly as an array of numbers. Windows Configuration: Windows Server 2000 SP4 (IIS 5) Php 5.2.3 (run as ISAPI mode, not cgi-mode) Any ideas or would be HUGELY appreciated. I am HOPING that there is some configuration switch that i have overlooked that will fix this bug. Otherwise, i will have to do a LOT of workarounds... thanks in advance, ynot2k |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
"AirYT" <airyt@PLEASEDONTSPAMMEfunkychickens.org> wrote in message news:Qutsj.233136$X56.208942@fe06.news.easynews.co m... > Howdy - found an interesting one here. I am trying to create an array of > objects. I've got some well formed classes and in some cases all i need to > do is to create an array of a particular class. after a few hours of > pulling the hair out i think i have found a bug in the windows version of > php (after i have tested it against a linux installation): > > here is the code: > <? > class t1 { > var $num; > } > $x = new t1(); > for ($c=0;$c<5;$c++) { > $x->num = $c; > $Array[] = $x; > } > echo "<pre>"; > print_r( $Array ); > echo "</pre>"; > ?> > > LINUX OUTPUT (as expected): > Array > ( > [0] => t1 Object > ( > [num] => 0 > ) > > [1] => t1 Object > ( > [num] => 1 > ) > > [2] => t1 Object > ( > [num] => 2 > ) > > [3] => t1 Object > ( > [num] => 3 > ) > > [4] => t1 Object > ( > [num] => 4 > ) > > ) > > > WINDOWS OUTPUT (WTF?!): > Array > ( > [0] => t1 Object > ( > [num] => 4 > ) > > [1] => t1 Object > ( > [num] => 4 > ) > > [2] => t1 Object > ( > [num] => 4 > ) > > [3] => t1 Object > ( > [num] => 4 > ) > > [4] => t1 Object > ( > [num] => 4 > ) > > ) > > > So as you can see, each object in the windows array has been replaced with > the last object that was assigned to the array. > If i display the array as it is being built on each iteration of the loop, > you can see quite clearly that the previously added objects are being > overwritten with the last-added object. > Thus you end up with an array with the correct number of objects, but each > object is simply a copy of the last object added to the array. > In this test example, if i add the 'num' value to the array instead (ie. > $Array = $x->num), it works perfectly as an array of numbers. > > Windows Configuration: > Windows Server 2000 SP4 (IIS 5) > Php 5.2.3 (run as ISAPI mode, not cgi-mode) > > Any ideas or would be HUGELY appreciated. I am HOPING that there is > some configuration switch that i have overlooked that will fix this bug. > Otherwise, i will have to do a LOT of workarounds... > > thanks in advance, ynot2k > > one thing i should note is that the Linux/Apache version of PHP is 4.3.2. So is this a PHP 5 issue instead? cheers, ynot2k |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
AirYT wrote:
> "AirYT" <airyt@PLEASEDONTSPAMMEfunkychickens.org> wrote in message > news:Qutsj.233136$X56.208942@fe06.news.easynews.co m... >> Howdy - found an interesting one here. I am trying to create an array of >> objects. I've got some well formed classes and in some cases all i need to >> do is to create an array of a particular class. after a few hours of >> pulling the hair out i think i have found a bug in the windows version of >> php (after i have tested it against a linux installation): >> >> here is the code: >> <? >> class t1 { >> var $num; >> } >> $x = new t1(); >> for ($c=0;$c<5;$c++) { >> $x->num = $c; >> $Array[] = $x; >> } >> echo "<pre>"; >> print_r( $Array ); >> echo "</pre>"; >> ?> >> >> LINUX OUTPUT (as expected): >> Array >> ( >> [0] => t1 Object >> ( >> [num] => 0 >> ) >> >> [1] => t1 Object >> ( >> [num] => 1 >> ) >> >> [2] => t1 Object >> ( >> [num] => 2 >> ) >> >> [3] => t1 Object >> ( >> [num] => 3 >> ) >> >> [4] => t1 Object >> ( >> [num] => 4 >> ) >> >> ) >> >> >> WINDOWS OUTPUT (WTF?!): >> Array >> ( >> [0] => t1 Object >> ( >> [num] => 4 >> ) >> >> [1] => t1 Object >> ( >> [num] => 4 >> ) >> >> [2] => t1 Object >> ( >> [num] => 4 >> ) >> >> [3] => t1 Object >> ( >> [num] => 4 >> ) >> >> [4] => t1 Object >> ( >> [num] => 4 >> ) >> >> ) >> >> >> So as you can see, each object in the windows array has been replaced with >> the last object that was assigned to the array. >> If i display the array as it is being built on each iteration of the loop, >> you can see quite clearly that the previously added objects are being >> overwritten with the last-added object. >> Thus you end up with an array with the correct number of objects, but each >> object is simply a copy of the last object added to the array. >> In this test example, if i add the 'num' value to the array instead (ie. >> $Array = $x->num), it works perfectly as an array of numbers. >> >> Windows Configuration: >> Windows Server 2000 SP4 (IIS 5) >> Php 5.2.3 (run as ISAPI mode, not cgi-mode) >> >> Any ideas or would be HUGELY appreciated. I am HOPING that there is >> some configuration switch that i have overlooked that will fix this bug. >> Otherwise, i will have to do a LOT of workarounds... >> >> thanks in advance, ynot2k >> >> > > one thing i should note is that the Linux/Apache version of PHP is 4.3.2. > So is this a PHP 5 issue instead? > > cheers, ynot2k > > > Yep, PHP4 copies objects; PHP5 references objects. Check out clone. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message news:Jvednd9CRpQS-i_anZ2dnUVZ_qLinZ2d@comcast.com... > AirYT wrote: >> "AirYT" <airyt@PLEASEDONTSPAMMEfunkychickens.org> wrote in message >> news:Qutsj.233136$X56.208942@fe06.news.easynews.co m... >>> Howdy - found an interesting one here. I am trying to create an array of >>> objects. I've got some well formed classes and in some cases all i need >>> to do is to create an array of a particular class. after a few hours of >>> pulling the hair out i think i have found a bug in the windows version >>> of php (after i have tested it against a linux installation): >>> >>> here is the code: >>> <? >>> class t1 { >>> var $num; >>> } >>> $x = new t1(); >>> for ($c=0;$c<5;$c++) { >>> $x->num = $c; >>> $Array[] = $x; >>> } >>> echo "<pre>"; >>> print_r( $Array ); >>> echo "</pre>"; >>> ?> >>> >>> LINUX OUTPUT (as expected): >>> Array >>> ( >>> [0] => t1 Object >>> ( >>> [num] => 0 >>> ) >>> >>> [1] => t1 Object >>> ( >>> [num] => 1 >>> ) >>> >>> [2] => t1 Object >>> ( >>> [num] => 2 >>> ) >>> >>> [3] => t1 Object >>> ( >>> [num] => 3 >>> ) >>> >>> [4] => t1 Object >>> ( >>> [num] => 4 >>> ) >>> >>> ) >>> >>> >>> WINDOWS OUTPUT (WTF?!): >>> Array >>> ( >>> [0] => t1 Object >>> ( >>> [num] => 4 >>> ) >>> >>> [1] => t1 Object >>> ( >>> [num] => 4 >>> ) >>> >>> [2] => t1 Object >>> ( >>> [num] => 4 >>> ) >>> >>> [3] => t1 Object >>> ( >>> [num] => 4 >>> ) >>> >>> [4] => t1 Object >>> ( >>> [num] => 4 >>> ) >>> >>> ) >>> >>> >>> So as you can see, each object in the windows array has been replaced >>> with the last object that was assigned to the array. >>> If i display the array as it is being built on each iteration of the >>> loop, you can see quite clearly that the previously added objects are >>> being overwritten with the last-added object. >>> Thus you end up with an array with the correct number of objects, but >>> each object is simply a copy of the last object added to the array. >>> In this test example, if i add the 'num' value to the array instead (ie. >>> $Array = $x->num), it works perfectly as an array of numbers. >>> >>> Windows Configuration: >>> Windows Server 2000 SP4 (IIS 5) >>> Php 5.2.3 (run as ISAPI mode, not cgi-mode) >>> >>> Any ideas or would be HUGELY appreciated. I am HOPING that there is >>> some configuration switch that i have overlooked that will fix this bug. >>> Otherwise, i will have to do a LOT of workarounds... >>> >>> thanks in advance, ynot2k >>> >>> >> >> one thing i should note is that the Linux/Apache version of PHP is 4.3.2. >> So is this a PHP 5 issue instead? >> >> cheers, ynot2k > > Yep, PHP4 copies objects; PHP5 references objects. Check out clone. > > -- > ================== > Remove the "x" from my email address > Jerry Stuckle > JDS Computer Training Corp. > jstucklex@attglobal.net > ================== > bingo. grazie, ynot2k |
|
![]() |
| Outils de la discussion | |
|
|