What are the pros and cons of using fopen
as opposed to SplFileObject
in PHP?
From what I see, SplFileObject
throws exceptions where applicable which makes this convenient when using try...catch
for error handling. Apart from this, are there any other reasons to recommend one over the other?
(Bonus: Why is it called SplFileObject
? What does Spl
stand for? Why not simply FileObject
?)
Update: One limitation of the SplFileObject
is that it does not (yet) have a close
member function. In some scenarios this can be a problem (Example: Unlink and SplFileObject).
Spl is a standard library in Php making a lot of nice stuff.
One good reason using a SplFileObject is that it makes your code Object Oriented. If you want, you can extend the class in your own code base and make it do some nice stuff while using files, with already the basics covered.