From 6e9fd0f7d1d8a68daf60af6a13d6c2fee2c1481b Mon Sep 17 00:00:00 2001 From: Gwynne Raskind Date: Sat, 17 May 2008 06:53:19 +0000 Subject: [PATCH] First part of module globals docs git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@259858 c90b9560-bf6c-de11-be94-00142212c4b1 --- internals2/structure/globals.xml | 70 +++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/internals2/structure/globals.xml b/internals2/structure/globals.xml index 1de903aba4..5e7af9bcec 100644 --- a/internals2/structure/globals.xml +++ b/internals2/structure/globals.xml @@ -1,8 +1,74 @@ - + Extension globals - + + + Introduction to globals in a PHP extension + + + In a language such as C, a "global" variable is a variable that can be + 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 + be accessed and changed by any piece of code anywhere in the program, + 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. + The "counter" extension is a perfect example of this need: The basic + interface calls for a counter with a persistant value. A programmer new to + 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 + + + + + + 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 + which more than one copy of PHP is running in the same thread, which means + more than one instance of the counter module. Suddenly these multiple + threads are sharing the same counter value, which is clearly undesireable. + Another problem shows itself when considering that another extension might + someday happen to have a global with the same name, and due to the rules of + C scoping, this has the potential to cause a compile failure, or worse, a + runtime error. Something more elaborate is needed, and so exists Zend's + support for threadsafe per-module globals. + + + +