PHP/resource: Difference between revisions

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP
Created page with "==About== In PHP, a "resource" represents an opaque internal class for handling certain types of I/O-ish operations. Tentatively, variables may be declared to be of type..."
 
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{fmt/title|the "resource" data-type in PHP}}
==About==
==About==
In [[PHP]], a "resource" represents an opaque internal class for handling certain types of I/O-ish operations.
In [[PHP]], a "resource" value is an opaque internal pointer for handling certain types of I/O-ish operations.
 
===Syntax===
Tentatively, variables may be declared to be of type "<code>\resource</code>" (as of PHP 8 or so).
As of PHP 8.5, although "resource" is "[https://www.php.net/manual/en/reserved.other-reserved-words.php soft-reserved] and is used as a documentation pseudotype,  there is no actual "resource" type; the only type which is currently known to be compatible with resource variables is "[https://www.php.net/manual/en/language.types.mixed.php <code>mixed</code>]". PHP currently interprets invocation of "<code>resource</code>" as a type as a reference to an unknown class named "{{fmt/arg|current namespace}}<code>\resource</code>".
===Types===
There are many specialty resource-types defined by extensions, and a handful of more general types:
{| class=wikitable
! type || subtype || functions
{{!-!}} [[/curl/]] || cURL handle
{{!-!}} [[/process/]] || || <code>[https://www.php.net/manual/en/function.proc-open.php proc_open()], [https://www.php.net/manual/en/function.proc-get-status.php proc_get_status()], [https://www.php.net/manual/en/function.proc-terminate.php proc_terminate()], [https://www.php.net/manual/en/function.proc-close.php proc_close()]</code>
{{!-!}} [[/stream/]] || dir handle
{{!-!}} [[/stream/]] || file handle
{{!-!}} [[/stream/]] || process handle
{{!-!}} [[/socket/]] || file handle
{{!-!}} [[/ssh2/]] session
{{!-!}} [[/ssh2/]] listener
{{!-!}} [[/ssh2/]] SFTP
{{!-!}} [[/ssh2/]] publickey system
|}
Source: [https://www.php.net/manual/en/resource.php List of Resource Types]
===Advanced===
New [https://www.php.net/manual/en/book.stream.php stream] resource types can be defined in code with [https://www.php.net/manual/en/function.stream-wrapper-register.php <code>stream_wrapper_register()</code>].
==Functions==
These are just the general functions that apply to all resources; there are many others that do things with specific types of resources. In general, this is all not very well documented (there is plenty of documentation, but very little of it explains how things work).
* [https://www.php.net/manual/en/function.get-resource-id.php <code>get_resource_id()</code>]
* [https://www.php.net/manual/en/function.get-resource-type.php <code>get_resource_type()</code>]
* [https://www.php.net/manual/en/function.is-resource.php <code>is_resource()</code>]
* If passed a resource variable, [https://www.php.net/manual/en/function.get-debug-type.php <code>get_debug_type()</code>] will indicate whether it is open or closed. (Surely there is some more rigorous way to get this information, however.)
==Links==
==Links==
===Reference===
===Reference===
* [https://www.php.net/manual/en/language.types.resource.php Resources]
* [https://www.php.net/manual/en/language.types.resource.php Resources]
** [https://www.php.net/manual/en/resource.php List of Resource Types]
** [https://www.php.net/manual/en/resource.php List of Resource Types]

Latest revision as of 17:31, 10 February 2026

the "resource" data-type in PHP

{{#set: page title=the "resource" data-type in PHP }}

About

In PHP, a "resource" value is an opaque internal pointer for handling certain types of I/O-ish operations.

Syntax

As of PHP 8.5, although "resource" is "soft-reserved and is used as a documentation pseudotype, there is no actual "resource" type; the only type which is currently known to be compatible with resource variables is "mixed". PHP currently interprets invocation of "resource" as a type as a reference to an unknown class named "<current namespace>\resource".

Types

There are many specialty resource-types defined by extensions, and a handful of more general types:

type subtype functions
curl cURL handle
process proc_open(), proc_get_status(), proc_terminate(), proc_close()
stream dir handle
stream file handle
stream process handle
socket file handle
ssh2 session
ssh2 listener
ssh2 SFTP
ssh2 publickey system

Source: List of Resource Types

Advanced

New stream resource types can be defined in code with stream_wrapper_register().

Functions

These are just the general functions that apply to all resources; there are many others that do things with specific types of resources. In general, this is all not very well documented (there is plenty of documentation, but very little of it explains how things work).

Reference