Understanding the shift from resources to objects
The PHP language has always had an uneasy relationship with resources. Resources represent a connection to an external system such as a file handle or a connection to a remote web service using the client URL (cURL) extension. One big problem with resources, however, is that they defy attempts at data typing. There's no way to distinguish a file handle from a cURL
connection—they're both identified as resources.
In PHP 8, a major effort has taken place to move away from resources and to replace them with objects. One of the earliest examples of this trend prior to PHP 8 is the PDO
class. When you create a PDO
instance, it automatically creates a database connection. Starting with PHP 8, many functions that previously produced a resource now produce an object instance instead. Let's start our discussion by having a look at extension functions that now produce objects rather than resources.