I spent an hour trying to create mock setup for testing SQL queries. The explanation here, that a resource contains file handlers and therefore there is no sense in trying to create one is lame. Being unable to redefine functions, creating a fake resource was the second thing I tried to put test in place, but looking at the search results, I see I'm the first one to try... For me it looks like security by obscurity.
Resource
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.
Note: The resource type was introduced in PHP 4
See also get_resource_type().
Converting to resource
As resource types hold special handlers to opened files, database connections, image canvas areas and the like, you cannot convert any value to a resource.
Freeing resources
Due to the reference-counting system introduced with PHP 4's Zend Engine, it is automatically detected when a resource is no longer referred to (just like Java). When this is the case, all resources that were in use for this resource are made free by the garbage collector. For this reason, it is rarely ever necessary to free the memory manually by using some free_result function.
Note: Persistent database links are special, they are not destroyed by the garbage collector. See also the section about persistent connections.
Resource
07-Jul-2008 08:55
16-Aug-2004 10:25
In response to yasuo_ohgaki, the reason for the inability of the $_SESSION[] variable to hold references is because a session is just a serialize()'d version of it's member variables saved under a unique filename, with this filename following the user around.
$_SESSION[] is therefore limited by the constraints of the serialize() function
Although this is not <i>strictly</i> true, ($_SESSION does some handling to convert messy variables (e.g. "s and ;s)) it cannot store resources due to the serialise() function's dependancy
22-Jun-2002 01:37
For the the oblivious: An example of a resource would be a mysql database connection.
$result = mysql_connect("localhost", "username", "pass");
//$result variable is a resource.
print $result;
//will print: Resource ID#1, or something similar
