reference appendices

Resource Types

A PHP resource is a handle to an external asset such as an open file, stream, or process. Resources are not ordinary objects, and their lifecycle matters in long-running processes.

Use This Reference When

  • Opening and closing files.
  • Reading legacy extension APIs that return resources.
  • Debugging leaked handles in workers or batch jobs.

Close an Open File

PHP example
<?php

$handle = fopen('php://memory', 'w+');
fwrite($handle, "php\n");
rewind($handle);
echo stream_get_contents($handle);
fclose($handle);

// Prints:
// php

Check each function’s return type because many resource-producing APIs return false on failure. Close resources promptly when ownership is clear.

Practice

Handle an Open Failure

Open a file for reading, check the return value, and close the handle after use. Describe which layer owns cleanup.

Show solution

Check if ($handle === false) before reading. The code that successfully opens the handle should usually ensure it is closed, often with finally when later operations can fail.