If fclose closes a file, how do you close an open file with the SplFileObject object?

3

When we open a file with fopen , we use the function fclose to close the handle of that file.

 $handle = fopen('file.txt', 'r');

 fclose($handle);

But my curiosity is: And when we use the object SplFileObject ? It does not have the fclose method.

How does the file close when we instantiate this class to open it?

$file = new SplFileObject('file.txt', 'r');

$file-> // Como faço para fechar?
    
asked by anonymous 26.04.2016 / 21:42

1 answer

4

Just set it to null

$file = new SplFileObject('file.txt', 'r');

$file = null;

In general objects have a cleanup in the destructor, the fact of setting the variable to null already does what is needed in many cases. And even without a destructor, the GC collects the resources normally at some point (attention, read through).

Note that this does not necessarily apply to any class. To be sure, you need to see the documentation for the specific class, or review the implementation if you have access to the sources.

In addition, according to a comment in the manual, by having an undocumented private property that holds the file pointer , the files become stuck in the object's existence.

  

link

    
26.04.2016 / 21:46