PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > array of objects in linux vs windows (bug?)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
array of objects in linux vs windows (bug?)

Réponse
 
LinkBack Outils de la discussion
Vieux 13/02/2008, 03h29   #1
AirYT
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut array of objects in linux vs windows (bug?)

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


  Réponse avec citation
Vieux 13/02/2008, 03h36   #2
AirYT
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array of objects in linux vs windows (bug?)


"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


  Réponse avec citation
Vieux 13/02/2008, 03h42   #3
Jerry Stuckle
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array of objects in linux vs windows (bug?)

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
==================

  Réponse avec citation
Vieux 13/02/2008, 03h48   #4
AirYT
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array of objects in linux vs windows (bug?)


"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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h47.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14622 seconds with 12 queries