PHP Internal question - Local variables

General discussion on PHP
Post Reply
test1636
Posts: 6
Joined: Tue Jun 14, 2016 10:47 pm

PHP Internal question - Local variables

Post by test1636 » Tue Jun 14, 2016 10:52 pm

I am writing a PHP extension.
I have implemented 4 functions in this extension:

PHP_MINIT_FUNCTION
PHP_MSHUTDOWN_FUNCTION
PHP_RINIT_FUNCTION
PHP_RSHUTDOWN_FUNCTION

It works fine: The first M functions are called when the php module is created/shutdown
The R functions are called when the request is created/shutodwn

I want to store data when a request is created (PHP_RINIT_FUNCTION) and i want to read this data when the request will be finished (in PHP_RSHUTDOWN_FUNCTION)

As you can imagine, i have a lot of request on my server. Some request are working in parallel.

When i create a variable in my extension, this variable will be shared for all the module life. A module can handle several requests. So i do not know how i should store this data. I need to keep scope between each RINIT/RSHUTDOWN.

I have also tried ZEND_DECLARE_MODULE_GLOBALS but the variable are also shared for all the module

Thanks a lot

User avatar
eranb2
Posts: 10
Joined: Sun Jan 18, 2009 10:40 am

Re: PHP Internal question - Local variables

Post by eranb2 » Wed Jun 15, 2016 8:30 am

Hi,

You can always share a static class / struct and save the data to this class.
This class is accessible from both the RINIT and RSHUTDOWN functions of PHP.

Note that if your PHP build is thread safe, then you will need to ensure that accessing that class is thread safe as well.

For example:

Code: Select all

// Assuming C++ here, but you can use C code as well ofc
#include <map>
#include <string>
static std::map<std::string, std::string> myMap;

PHP_RINIT_FUNCTION(MyExtension)
{
    myMap["hello"] = "world";
    return SUCCESS;
}

PHP_RSHUTDOWN_FUNCTION(MyExtension)
{
    // You can access myMap here as well...

    // Cleanup
    myMap.clear();
    return SUCCESS;
}
HTH,
Eran

test1636
Posts: 6
Joined: Tue Jun 14, 2016 10:47 pm

Re: PHP Internal question - Local variables

Post by test1636 » Wed Jun 15, 2016 1:18 pm

Thanks Eran,

In your case, myMap structure will be shared for all requests of the module.
But i am wondering if PHP can handle multiple request at the same time in the same module ?

Thanks

Post Reply