get_resource_type / get_resource_id / get_resources

get_resource_type(Resource $resource): string / get_resource_id(Resource $resource): int / get_resources(?string $type = null): array

Resource introspection: get_resource_type reads the handle's type name (e.g. stream), get_resource_id its numeric id, and get_resources lists currently live resources (optionally filtered by type; debugging tool).

C C23

C23
#include <stdio.h>

/* C handles are typed pointers (FILE *): no runtime type query exists,
   the portable probe is the behavior of the handle itself */
int main(void) {
    FILE *fp = tmpfile(); /* in-memory temporary: environment-independent */
    if (fp == NULL) return 1;
    fputs("x", fp);
    printf("handle non-null=%d\n", fp != NULL);
    printf("rewound read=%d\n", (rewind(fp), fgetc(fp)));
    fclose(fp);
    return 0;
}
Output
handle non-null=1
rewound read=120

C++ C++26

C++26
#include <iostream>
#include <sstream>

int main() {
    // iostreams carry their state: good()/fail() replace resource-type introspection
    std::istringstream in("data");
    std::cout << "good=" << in.good() << "; fail=" << in.fail() << '\n';
    in.get(); // consume one char
    std::cout << "after read good=" << in.good() << '\n';
    return 0;
}
Output
good=1; fail=0
after read good=1

C# v11.0.100

v11.0.100
using System;
using System.IO;

var stream = new MemoryStream(); // in-memory: environment-independent
Console.WriteLine($"CanRead={stream.CanRead}, CanSeek={stream.CanSeek}");
Console.WriteLine($"is MemoryStream={stream is MemoryStream}");
Console.WriteLine($"is Stream={stream is Stream}");
Output
CanRead=True, CanSeek=True
is MemoryStream=True
is Stream=True

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"io"
	"strings"
)

func main() {
	// Go handles are interfaces: type assertion replaces resource-type introspection
	var r io.Reader = strings.NewReader("data")
	if sr, ok := r.(*strings.Reader); ok {
		fmt.Println("concrete type ok=", ok, "; len=", sr.Len())
	}
	fmt.Println("is io.Reader=", r != nil)
}
Output
concrete type ok= true ; len= 4
is io.Reader= true

Java v27.0.1

v27.0.1
import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class Main {
    public static void main(String[] args) {
        // Java handles are typed interfaces: instanceof replaces resource-type queries
        InputStream in = new ByteArrayInputStream(new byte[0]); // in-memory: no filesystem
        System.out.println("is InputStream=" + (in instanceof InputStream));
        System.out.println("is ByteArrayInputStream=" + (in instanceof ByteArrayInputStream));
        System.out.println("marked supported=" + in.markSupported());
    }
}
Output
is InputStream=true
is ByteArrayInputStream=true
marked supported=true

JavaScript v26.13.1

v26.13.1
// JS has no opaque resource type: runtime type probes are Buffer.isBuffer / instanceof
const { Buffer } = require("node:buffer");
const buf = Buffer.alloc(8); // zero-initialized buffer
console.log("isBuffer=", Buffer.isBuffer(buf), "; zero-filled=", buf[0] === 0);
console.log("Uint8Array=", buf instanceof Uint8Array);
console.log("byte length=", buf.length);
Output
isBuffer= true ; zero-filled= true
Uint8Array= true
byte length= 8

PHP v8.6.0

v8.6.0
// resources are PHP's opaque handles: introspect the type and id, never
// dereference them; file handles report "stream".
$fp = fopen('php://memory', 'w+b'); // memory stream: environment-independent
echo 'type=', get_resource_type($fp), '; id int=', is_int(get_resource_id($fp)) ? 'yes' : 'no', "\n";
echo 'streams alive=', count(get_resources('stream')) >= 1 ? '>=1' : '0', "\n";
var_dump(get_resource_type($fp) === 'stream');
Output
type=stream; id int=yes
streams alive=>=1
bool(true)

Python v3.15.2

v3.15.2
# Python has no opaque resource type: file objects expose the OS fd via fileno()
import io

buf = io.BytesIO(b"data") # in-memory stream: environment-independent
print("readable=", buf.readable(), "; seekable=", buf.seekable())
print("BytesIO has no fd=", hasattr(buf, "fileno"))
Output
readable= True ; seekable= True
BytesIO has no fd= True

Rust v1.99.1

v1.99.1
use std::any::Any;
use std::io::Cursor;

fn main() {
    // Rust introspects concrete types via Any::downcast_ref: the closest tool
    let buf: Cursor<Vec<u8>> = Cursor::new(Vec::new());
    let a: &dyn Any = &buf;
    println!("downcast to Cursor={}", a.downcast_ref::<Cursor<Vec<u8>>>().is_some());
    println!("in-memory reader len={}", buf.get_ref().len());
}
Output
downcast to Cursor=true
in-memory reader len=0

TypeScript v26.13.1

v26.13.1
// JS has no opaque resource type: runtime type probes are Buffer.isBuffer / instanceof
import { Buffer } from "node:buffer";
const buf: Buffer = Buffer.alloc(8); // zero-initialized buffer
console.log("isBuffer=", Buffer.isBuffer(buf), "; zero-filled=", buf[0] === 0);
console.log("Uint8Array=", buf instanceof Uint8Array);
console.log("byte length=", buf.length);
Output
isBuffer= true ; zero-filled= true
Uint8Array= true
byte length= 8

Language differences

PHP: resource is PHP's own opaque handle type (a Resource type declaration exists since 8.0) — the in-PHP representation of the outside world (files/sockets/images/DB connections): introspectable but never dereferenceable; get_resource_type yields the type name (file handles are always stream), get_resource_id (8.0+) the numeric id (the old (int)$res cast idiom of PHP 7 and earlier is gone), and get_resources lists the whole table (the manual explicitly limits it to debugging/testing — not for production, and do not manipulate the stream underneath SplFileObject); pairs with is_scalar_object_resource (see that entry): is_resource decides, this entry introspects; after fclose the handle is dead (still typed resource but Unknown since 8.0). Cross-platform cautions: id ordering is unspecified and the id is NOT a file descriptor.

C: FILE* is a typed pointer — no runtime type query exists; probe via the handle's own behavior (if data is readable after rewind, it works); tmpfile (the example) is the standard library's in-memory temporary, environment-independent.

C++: iostreams carry state bits (good/fail/eof/bad) — state queries replace type queries; the concrete stream type is known at compile time, so at runtime you ask whether it is healthy.

C#: handles form a class hierarchy (Stream base + MemoryStream/FileStream subclasses) — is pattern matching decides the type, capability properties like CanRead/CanSeek introspect behavior.

Go: handles are interfaces (io.Reader/io.Writer) — one type assertion r.(*strings.Reader) yields the concrete type and value; there is no global handle table.

Java: handles are interface families (InputStream/OutputStream…) — instanceof decides the implementation and capability methods like markSupported() introspect behavior; no runtime handle inventory.

JavaScript: no opaque resource — Node's underlying fd is a plain int (from fs.openSync) and the upper handle is an object (Buffer/stream.Readable); probe with Buffer.isBuffer/instanceof; Buffer.alloc is the standard zero-initialized buffer.

Python: no opaque resource — file objects expose the OS fd directly (fileno(); in-memory streams like BytesIO have none, probe with hasattr) and readable()/seekable() introspect capabilities; the object itself carries the full type.

Rust: no opaque handles — types are known at compile time and trait objects need Any::downcast_ref to probe the concrete type downward; File/TcpStream close on Drop (RAII), with no leaked-handle table.

TypeScript: same as JavaScript; Buffer/Readable annotations give probes compile-time signatures while runtime behavior matches JS.

Teaching anchor: resource is the ten-language set's only non-dereferenceable handle abstraction, unique to PHP — in the other nine languages a handle is either a typed pointer/object (directly introspectable) or a raw fd (an integer); three sentences cover PHP resource's whole boundary: the type name is readable, the inside is invisible, and the id is not a descriptor.