NYCPHP Meetup

NYPHP.org

[nycphp-talk] Best way to copy array by value

Aaron Fischer agfische at email.smith.edu
Mon Feb 26 10:30:31 EST 2007


I think he was making an assumption about PHP based on how other  
languages work.  Everyone in the class is using Python or Java.  He  
is letting me use PHP, which hasn't been done before in this class.

Thanks for the tip on objects, I'll keep that in mind.

-Aaron

On Feb 26, 2007, at 10:16 AM, Brian Dailey wrote:

> If your professor was referring to PHP, it's possible he could have  
> been referring to objects, not arrays. Objects are copied by  
> reference, so if you do this:
>
> $Item = new Item;
> $Item2 = $Item;
> $Item2->setProductId('12');
> print $Item->getProductId();
>
> Your output will be '12'. If you want to copy an object in PHP 5  
> you'll need to use 'clone.'
>
> Aaron Fischer wrote:
>> Indeed it does.  Sorry!  I was led to believe (by my cs professor)  
>> that the default behavior when copying an array is that the copied  
>> array is not a new instance but is tied by pointers to the old  
>> array, so changes to one would change both of them.



More information about the talk mailing list