Re: Problem with Object references in an array via for loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well, I did that except I'm providing the key explicitly. However, I've tried it now without the key and it's the same except for the key value. Very frustrating.

Dave

Gabriel Sosa wrote:
I thought this line it may help, not actually the null
$secs[] =  $obj;

i will still researching about this

On Thu, May 29, 2008 at 2:08 PM, David Moylan <dave@xxxxxxxxxxxxx> wrote:
Nope.  Same performance.  I originally had something like this (minus the
assigment to null) but I thought the confusion was in the variable
assignment so I went directly to the array.  I've tried your code exactly
and get the same thing, assignment to null doesn't help.

Dave

Gabriel Sosa wrote:
try doing this

$secs = array();
foreach($_GET['sids'] as $sid){
 $obj = null;
 $obj = new CustomSecurity();
 $obj->set(array('customSecurityID' => $sid));
 $secs[] =  $obj;
}


i just don't know if the operator -> it's working wll over object arrays


saludos

On Thu, May 29, 2008 at 1:31 PM, David Moylan <dave@xxxxxxxxxxxxx> wrote:
I have a snippet of code like this which works fine in PHP4.


$secs = array();
foreach($_GET['sids'] as $sid){
 $secs[$sid] = new CustomSecurity();
 $secs[$sid]->set(array('customSecurityID' => $sid));
}

However, in PHP5 (5.2.4) the array has the proper keys in it but all of
the
array values in $secs seem to point to the same instance. Basically, the
customSecurityID value inside each array value (object instance) should
match the array key, but all of the customSecurityID values are set to
the
last $sid value during the final iteration which tells me the array
values
are not 3 separate instances, but references to the same instance.

I'm having a hard time seeing how this isn't a bug.  On each iteration of
the loop I'm using a new variable $secs[$sid] which is previously
unassigned
and assigning it via the new operator to a new instance.

Is this a bug or a strange reference behavior that I don't understand?

How can you fill arrays with multiple new instances?

Thanks.

Dave

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php







--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux