Writing a PHP Extension |
|
2024-11-26 |
|
|
15 |
|
|
If you need extension-wide global variables, you can't just use standard
C-style static variables because PHP needs to be thread-safe. To achieve this,
globals are placed in a struct and passed around. To add global variables to
your extension, first define them in your header file:
Then in your .c file:
And in your MINIT function:
Now, anywhere you need to get/set one of these variables, use
TEST_G(some_integer). This will only
work if the test_globals struct is available in the function, of course. All
the standard PHP functions will automatically have the globals struct
available, but if you write your own utility functions you either have to call
TSRMLS_FETCH(); at the top of the function, or better
yet, pass in the TSRM struct. Like this:
And the function is declared using: