Are PHP resource IDs unique per PHP instance? Does casting all kinds of resources to string returns “Resource id #X” (where X is some decimal number)? Is there a function that returns resource ID (I know about get_resource_type() to get resource typem, but didn’t find anything like get_resouce_id()), or does it have to be done like this?
function get_resource_id($resource)
{
return is_resource($resouce)
? substr((string) $resource, 13 /* strlen("Resource id #") */)
: NULL;
}
Jakub, yes PHP will always give you a unique ID for every resource that’s currently in use. When a resource is unset() or free’d such as mysql_free_result. Then that ID is available again and PHP could reuse these resources again.
Be wary if you’re “caching” or keeping resource ID’s in the session and reusing them because PHP may free a resource, then recreate a new resource with that ID and your old resource ID might be pointing to something new.
So there you have it, resources are unique, but not forever!
Hope this helps demystify resource types in PHP for you.