From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Working on text files with SplFileObject

Working on text files with SplFileObject - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Working on text files with SplFileObject

- The SplFileObject class provides an object oriented interface to working with files. This is the class's documentation in the online PHP manual. If we scroll down to look at the available methods, it becomes obvious fairly quickly that many of them duplicate file system functions such as fgetcsv, fgets, fpassthru. These are all functions that have been part of PHP since PHP 4 so you might be wondering why bother creating object oriented versions. Well, there are two main reasons. The code is simpler to use and the SplFileObject class extends SplFileInfo. As a result, the object inherits all the SplFileInfo methods as well. So let's create some code to experiment with SplFileObject. This is fileobject.php which you can find in the chapter two folder of the exercise files. It contains a FilesystemIterator that loops over the files in the common/documents directory, and then on line four, a conditional statement uses the getExtension method to find only text files. If we load this page…

Contents