From 51eca8e6ea66558d9f1d7791381b98e045c5c3ef Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Fri, 6 Jun 2008 10:07:37 +0000 Subject: [PATCH] add some whitespace, add section on accessing globals git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@260840 c90b9560-bf6c-de11-be94-00142212c4b1 --- internals2/structure/globals.xml | 72 +++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/internals2/structure/globals.xml b/internals2/structure/globals.xml index 798b5d0b81..f4b55742a2 100644 --- a/internals2/structure/globals.xml +++ b/internals2/structure/globals.xml @@ -1,5 +1,5 @@ - + Extension globals @@ -11,7 +11,9 @@ accessed from any function without any extra declaration. These traditional globals have a few drawbacks: + + Barring any special options passed to the compiler, a global varaible can @@ -19,17 +21,21 @@ whether or not that code should be doing so. + A typical global variable is not thread safe. + The names of global variables are as global as the variables themselves. + + A PHP extension's globals are more properly called the "extension state", since most modules must remember what they're doing between function calls. @@ -38,6 +44,7 @@ Zend and PHP might do something like this in counter.c to store that value: + The wrong way to store the basic counter interface's value @@ -54,6 +61,7 @@ PHP_FUNCTION(counter_get) ]]> + On the surface this appears a viable solution, and indeed in a simple test it would function correctly. However, there are a number of situations in @@ -83,6 +91,7 @@ PHP_FUNCTION(counter_get) module is simply "counter". Here is the global structure declaration from php_counter.h: + The counter module's globals @@ -93,9 +102,11 @@ ZEND_END_MODULE_GLOBALS(counter) ]]> + And this is the declaration from counter.c: + The counter module's global structure declaration @@ -106,6 +117,65 @@ ZEND_DECLARE_MODULE_GLOBALS(counter) + + + Accessing module globals + + + As discussed above, per-module globals are declared inside a C structure + whose name is obscured by Zend macros. As a result, the ideal way to access + members of this structure is by the use of further macros. Accordingly, most + if not all extensions which have globals have a declaration like this + somewhere in their header file: + + + + Accessor macros for per-module globals + + + + + + + + This could have been generalized into a macro of its own by the Zend API, + but as of PHP 5.3 (and PHP 6 at the time of this writing), that hasn't + happened. The global accessor construct is written into the header by + ext_skel and thus is generally left alone by extension + writers, unless they wish to change the name of the accessor macro. + + + + + + COUNTER_G was the name given to the macro by + ext_skel, but it's not necessary for it to have that + name and could just as easily be called FOO instead. + + + + + Any code in the counter extension that accesses a global must thus wrap it + in the macro COUNTER_G. + + + + + Any function which accesses globals must either be declared by Zend macros, + have TSRMLS_DC as its last argument, or call the macro + TSRMLS_FETCH before accessing the globals. See + the TSRM documentation for + more information. + + + +