International PHP Conference Berlin 2023


A resource is a special variable, holding a reference to an external resource. Resources are created and used by special functions. See the appendix for a listing of all these functions and the corresponding resource types.

See also the get_resource_type() function.

Converting to resource

As resource variables hold special handles to opened files, database connections, image canvas areas and the like, converting to a resource makes no sense.

Freeing resources

Thanks to the reference-counting system being part of Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.

Note: Persistent database links are an exception to this rule. They are not destroyed by the garbage collector. See the persistent connections section for more information.

add a note

User Contributed Notes 1 note

mmenzel at it-economics dot de
1 month ago
'stream' is a general resource type and can have specific subtypes (imap, pop, curl ...). Casting from 'stream' to them makes sense alright.

E.g. Making OAUTH2 work for imap, but still use the normal imap_ functions. Just open a ssl:// stream to the IMAP server with stream_socket_client(), send the "AUTHENTICATE XOAUTH2 ..." authentication with valid token and then use the imap_ functions on the casted 'stream' to 'imap' resource.
Not being able to cast from 'stream' to 'imap' makes it necessary to use 3rd party solutions, like php-imap. Doesn't have to be necessary, if the cast would be possible.
To Top