views:

28

answers:

1

It is possible to fetch the zend resources (zend_fetch_resource) without knowing the type of the fetching resource? If so, how?

Note: I am writing PHP extension.

A: 

Yes, you can.

zend_fetch_resource won't work because it receives the the types of resources that are acceptable and fails if the found one is not one of those.

Just use

void *zend_list_find(int id, int *type);

From the resource zval you can extract the id with Z_RESVAL(zval). The argument type will be filled with the type of the resource found.

However, I don't see much usage for this, except maybe to create a var_dump clone. The problem is that once you retrieve an arbitrary resource, what are you going to do with it?... In general, you know nothing about the returned data structure.

You can get the name of the resource directly with:

char *zend_rsrc_list_get_rsrc_type(int resource TSRMLS_DC);
Artefacto
Oh thank you. The use I need is that I am finishing the PECL Thread extensions (for CLI) because I want to create a server. But with server, I need to be able to pass resource between two thread, and I can't just copy zval from one thread to other, right? So I think I can fetch the resource into true global for storing, and when requested just register new zval resource with the same resource pointer (void*).
Nat
@Nat That won't work. The resource list is a thread local variable. See http://en.wikipedia.org/wiki/Inter-process_communication
Artefacto
What I am going to do, is to fetch the resource in the source thread, store the resource (in void* form) in true global (not zend's TSRM), and put back in zval in destination thread. This should work, eh? IPC is not possible because the resource is not serializable, is it?
Nat
@Nat You'd still have to synchronize access to the data pointed to by the resource pointer.
Artefacto
Yes, I use the lock mutex for both global resource storage and in-php resource using. Thank you very much for you help.
Nat