Reputation: 3081
In my script I need an open stream to a CSV file and my application will read single, many or all of its lines depending on different request, and store them in an array!
when and if we reach to the point that all lines been read and stored, the scrip will close the file.
but also i've added the close file method into the class destructor
public function __destruct()
{
fclose($this->handler)
}
I want to know if this is really necessary? or considering this is the end of lifecycle of my script, the file handler will be taken care by PHP's garbage collector and adding the above destruct method doesn't add any value?
Upvotes: 0
Views: 1068
Reputation: 4284
I preffer to close the file when no more writing is need or in an exception:
try {
//write file
//close file after all reading operations has been done
} catch (Exception $e) {
fclose($this->handler);
}
This is good as an emergency or an uncatched problem, because PHP will close the file after deleting the object. If not, PHP interpreter will free all resources, and in Files it will free the memory address and any pending writing won't be done.
Upvotes: 2