fgetcsv function in PHP inside member function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

fgetcsv function in PHP inside member function

I am trying to build a class in PHP that will accept a file in CSV format and eventually pull the CSV data apart and send it into various locations in a database. I have started by simply making 3 member functions for the class. one that opens the CSV file, one that reads a line from the CSV file, and then a function that closes the CSV file. If I use fgetcsv() function outside the class definition code, it works as excepted. for example (assume the file is already open): while ($row = fgetcsv($fileResource, 1000, ",")) { for ($i = 0; $i < count($row); $i++) { echo $row[$i]; } } This will read every line in the CSV file and output it. If I do: $row = fgetcsv($fileResource, 1000, ","); print_r($row); $row = fgetcsv($fileResource, 1000, ","); print_r($row); this will read the first line, output it, then read the second line, output it. If I make a member function to read a single line from the CSV file and hold it in a member variable, it doesn't work. public function readCSVRow() { $this->$arr_currentRow = fgetcsv($this->$file_CSV, 1000, ",", '"'); } If I try to access the $arr_CurrentRow member (it is declared public) it returns 1. I try to access it by doing: $objectName->$arr_currentRow where $objectName is the object of my class.

25th Jun 2019, 5:12 AM
Nathan Stanley
Nathan Stanley - avatar
2 Answers
+ 1
so I found out that the problem I had was this: $objectName->$memberName should be changed to: $objectName->memberName and it fixed it
30th Jun 2019, 12:01 AM
Nathan Stanley
Nathan Stanley - avatar
0
is there something about objects in PHP I should know that is preventing this from working?
25th Jun 2019, 10:15 PM
Nathan Stanley
Nathan Stanley - avatar