Handling Standard PHP Library (SPL) Changes

Learn how the SPL extension in PHP 8 provides fundamental data structures and improves OOP functionality.

The SPL is an extension that contains key classes that implement basic data structures and enhance OOP functionality. It was first introduced in PHP 5 and is now included by default in all PHP installations. We discuss where significant changes have occurred in the SPL when running PHP 8. In addition, we give you tips and guidance on SPL changes that have the potential to cause our existing applications to stop working.

We start by examining changes to the SplFileObject class.

Understanding changes to SplFileObject

SplFileObject is an excellent class that incorporates most of the standalone f*() functions, such as fgets(), fread(), fwrite(), and so forth, into a single class. SplFileObject ::__construct() method arguments mirror the arguments provided to the fopen() function.

The main difference in PHP 8 is that a relatively obscure method, fgetss(), has been removed from the SplFileObject class. The SplFileObject::fgetss() method, available in PHP 7 and below, mirrors the standalone fgetss() function in that it combines fgets() with strip_tags().

For the sake of illustration, let’s assume we have created a website that allows users to upload text files. Before displaying content from the text file, we wish to remove any markup tags. Here is an example that uses the fgetss() method to accomplish this:

Get hands-on with 1200+ tech skills courses.