# Sharing resources between modules

- Thread: https://externals.io/message/970
- Original archive: https://news-web.php.net/php.internals/970
- Started by Marshall A. Greenblatt on 2003-04-19T07:20:34+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #970 — Marshall A. Greenblatt — 2003-04-19T07:20:34+00:00

URL: https://externals.io/message/970#970

Hi guys, a quick question:

Is it possible for one module to retrieve the resource structure registered
by another module?

For instance, assume that we have two loaded PHP modules, `php_a.so' and
`php_b.so'.  A script calls a PHP function defined in `php_a.so'.  That
function creates a structure, calls zend_register_resource(), and returns
the resource ID.  That same script then calls a PHP function defined in
`php_b.so' that accepts the resource ID as a parameter.  Would it be
possible for the function in `php_b.so' to retrieve the structure associated
with the resource ID registered in `php_a.so'?


My thinking is that something like the following (semi-psudocode) should
work:

In the `php_a.so' function, which creates the resource:

   zval *myzvalp;
   mystruct *mystructp = emalloc(sizeof(mystruct));

   MAKE_STD_ZVAL(myzvalp);
   zend_register_resource(myzvalp, mystructp, "my structure");

   RETURN_RESOURCE(myzvalp);


In the `php_b.so' function, which retrieves the resource:

   int res_type;
   mystruct *mystructp;
   zval *myzvalp;

   [ get myzvalp from the parameter list ]

   res_type = zend_fetch_list_dtor_id("my structure");
   mystructp = (mystruct*)zend_fetch_resource(&myzvalp TSRMLS_CC,
                                              -1, "my structure",
                                              NULL, 1, res_type);
   if(mystructp) {
      [ mystructp should be the same as allocated in `php_a.so' ]
   }


However, I see the `TSRMLS_CC' value in the zend_fetch_resource() call and
this makes me wonder if zend_fetch_resource() is using something specific to
the current module.  Also, I'm guessing at the use of the
zend_fetch_list_dtor_id() function, so any suggestions about a more
appropriate way to do this (or a pointer to code where this is currently
being done) would be appreciated.

Thanks,
  Marshall

---

## #974 — Wez Furlong — 2003-04-19T10:51:36+00:00

URL: https://externals.io/message/970#974 · In reply to: #970

Yes, it is possible; you will need to know the list entry id in advance,
so it needs to be exported from one module either as a global variable
or as the return value from a function.

TSRMLS stuff is used in thread-safe builds as a means to reduce TLS
overhead; it will not affect you intended usage.

BTW: ZEND_REGISTER_RESOURCE and ZEND_FETCH_RESOURCE macros might make
your code slightly easier to write.

--Wez.



> Hi guys, a quick question:
>
> Is it possible for one module to retrieve the resource structure registered
> by another module?
>
> For instance, assume that we have two loaded PHP modules, `php_a.so' and
> `php_b.so'.  A script calls a PHP function defined in `php_a.so'.  That
> function creates a structure, calls zend_register_resource(), and returns
> the resource ID.  That same script then calls a PHP function defined in
> `php_b.so' that accepts the resource ID as a parameter.  Would it be
> possible for the function in `php_b.so' to retrieve the structure associated
> with the resource ID registered in `php_a.so'?

---

## #983 — Marshall A. Greenblatt — 2003-04-19T22:52:51+00:00

URL: https://externals.io/message/970#983 · In reply to: #970

----- Original Message -----
From: "Marshall A. Greenblatt" <marshall@magpcss.com>
To: <internals@lists.php.net>
Sent: Saturday, April 19, 2003 3:20 AM
Subject: [PHP-DEV] Sharing resources between modules


> Hi guys, a quick question:
>
> Is it possible for one module to retrieve the resource structure
registered
> by another module?
>
[snip]
>    res_type = zend_fetch_list_dtor_id("my structure");
>    mystructp = (mystruct*)zend_fetch_resource(&myzvalp TSRMLS_CC,
>                                               -1, "my structure",
>                                               NULL, 1, res_type);
[snip]

The zend_fetch_list_dtor_id() function works as I expected; it returns the
resource type ID for the specified resource name, irregardless of what
module
that ID was originally registered in.  As long as both modules know the name
of the resource, I don't have to export the value returned from
zend_register_list_destructors_ex().

Thanks for the response Wez!

- Marshall
