myext_globals->global_value vs MYEXT_G(global_value)

From: Date: Thu, 05 Aug 2010 01:23:05 +0000
Subject: myext_globals->global_value vs MYEXT_G(global_value)
Groups: php.pecl.dev 
Request: Send a blank email to pecl-dev+get-7408@lists.php.net to get a copy of this message
I am wondering about the correct way to deal with global vars when writing a PECL extension. I hope to have a myext_last_error sort of function where one might find the last error encountered.  In the JSON extension, I see such a global variable declared in php_json.h: ZEND_BEGIN_MODULE_GLOBALS(json) int error_code; ZEND_END_MODULE_GLOBALS(json) And then initialized in a function I have not yet seen documented in any of the 'getting started' materials: /* {{{ PHP_GINIT_FUNCTION */ static PHP_GINIT_FUNCTION(json) {     json_globals->error_code = 0; } /* }}} */ This json_globals->error_code assignment puzzles me for two reasons. First, what is this PHP_GINIT_FUNCTION? I've searched for it in the opengrok system and have seen it used in many places but this doesn't tell me when it is executed or what the function is for. Second, in the files generated by ext_skel, I see this: /* In every utility function you add that needs to use variables in php_fmog_globals, call TSRMLS_FETCH(); after declaring other variables used by that function, or better yet, pass in TSRMLS_CC after the last function argument and declare your utility function with TSRMLS_DC after the last declared argument. Always refer to the globals in your function as FMOG_G(variable). You are encouraged to rename these macros something shorter, see examples in any other php module directory. */ #ifdef ZTS #define FMOG_G(v) TSRMG(fmog_globals_id, zend_fmog_globals *, v) #else #define FMOG_G(v) (fmog_globals.v) #endif It seems to me that a last_error type var should be initiated in a MINIT function, no? Also, which is the correct way to reference this var? It is my desire to have a function which is global to the scope my file myext.c but I don't want any crosstalk between concurrent processes that might be running this code. Any insight would be very much appreciated.

« previous php.pecl.dev (#7408) next »