Skip to content
Advertisement

Problem with PHP object and foreach loop, object not properly storing values

I’m trying to get values from a file called input.csv and add it to an object called $myItem. I’m using a foreach method to do so, as you can see. For some reason that is unclear to me, the values of the object are getting mixed up.

When I initially set the value of $item_number using my setter method the value is the correct value – the first entry in the first row of input.csv. However, by the time the foreach loop is completed, the value has changed to the last entry in the first row of input.csv.

This is true for all the instance variables in the object $myItem, they’re all changing to the last entry in the first row of input.csv. I’m not sure why.

Here is my code:

JavaScript

Thanks in advance for any help!

Advertisement

Answer

Take a look at your setter methods, all of them are doing:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement