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

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

 



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



-- 
Los sabios buscan la sabiduría; los necios creen haberla encontrado.
Gabriel Sosa

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