Source of: /manual/en/internals2.structure.globals.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/internals2.structure.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'internals2.structure.globals.php',
1 => 'Extension globals',
),
'up' =>
array (
0 => 'internals2.structure.php',
1 => 'Extension structure',
),
'prev' =>
array (
0 => 'internals2.structure.modstruct.php',
1 => 'The zend_module structure',
),
'next' =>
array (
0 => 'internals2.structure.lifecycle.php',
1 => 'Life cycle of an extension',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="internals2.structure.globals" class="sect1">
<h2 class="title">Extension globals</h2>
<div id="internals2.structure.globals.intro" class="sect2">
<h3 class="title">Introduction to globals in a PHP extension</h3>
<p class="simpara">
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:
</p>
<ul class="itemizedlist">
<li class="listitem">
<span class="simpara">
Barring any special options passed to the compiler, a global variable can
be accessed and changed by any piece of code anywhere in the program,
whether or not that code should be doing so.
</span>
</li>
<li class="listitem">
<span class="simpara">
A typical global variable is not thread safe.
</span>
</li>
<li class="listitem">
<span class="simpara">
The names of global variables are as global as the variables themselves.
</span>
</li>
</ul>
<p class="simpara">
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 persistent value. A programmer new to
Zend and PHP might do something like this in <var class="filename">counter.c</var>
to store that value:
</p>
<div class="example">
<p><b>Example #1 The wrong way to store the basic counter interface's value</b></p>
<div class="example-contents programlisting">
<div class="ccode"><pre class="ccode">/* ... */
static long basic_counter_value;
/* ... */
PHP_FUNCTION(counter_get)
{
RETURN_LONG(basic_counter_value);
}</pre>
</div>
</div>
</div>
<p class="simpara">
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 undesirable.
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 thread-safe per-module globals.
</p>
</div>
<div id="internals2.structure.globals.declaring" class="sect2">
<h3 class="title">Declaring module globals</h3>
<p class="simpara">
Whether a module uses only a single global or dozens, they must be defined
in a structure, and that structure must be declared. There are some macros
that assist with doing so in a way that avoids name conflicts between
modules: <b>ZEND_BEGIN_MODULE_GLOBALS()</b>,
<b>ZEND_END_MODULE_GLOBALS()</b>, and
<b>ZEND_DECLARE_MODULE_GLOBALS()</b>. All three take as a
parameter the short name of the module, which in the case of the counter
module is simply <i>"counter"</i>. Here is the global structure
declaration from <var class="filename">php_counter.h</var>:
</p>
<div class="example">
<p><b>Example #2 The counter module's globals</b></p>
<div class="example-contents programlisting">
<div class="ccode"><pre class="ccode">ZEND_BEGIN_MODULE_GLOBALS(counter)
long basic_counter_value;
ZEND_END_MODULE_GLOBALS(counter)</pre>
</div>
</div>
</div>
<p class="simpara">
And this is the declaration from <var class="filename">counter.c</var>:
</p>
<div class="example">
<p><b>Example #3 The counter module's global structure declaration</b></p>
<div class="example-contents programlisting">
<div class="ccode"><pre class="ccode">ZEND_DECLARE_MODULE_GLOBALS(counter)</pre>
</div>
</div>
</div>
</div>
<div id="internals2.structure.globals.using" class="sect2">
<h3 class="title">Accessing module globals</h3>
<p class="simpara">
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:
</p>
<div class="example">
<p><b>Example #4 Accessor macros for per-module globals</b></p>
<div class="example-contents programlisting">
<div class="ccode"><pre class="ccode">#ifdef ZTS
#define COUNTER_G(v) TSRMG(counter_globals_id, zend_counter_globals *, v)
#else
#define COUNTER_G(v) (counter_globals.v)
#endif</pre>
</div>
</div>
</div>
<blockquote><p><b class="note">Note</b>:
<span class="simpara">
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
<strong class="command">ext_skel</strong> and thus is generally left alone by extension
writers, unless they wish to change the name of the accessor macro.
</span>
</p></blockquote>
<blockquote><p><b class="note">Note</b>:
<span class="simpara">
<b><tt class="constant">COUNTER_G</tt></b> was the name given to the macro by
<strong class="command">ext_skel</strong>, but it's not necessary for it to have that
name and could just as easily be called <i>FOO</i> instead.
</span>
</p></blockquote>
<p class="simpara">
Any code in the counter extension that accesses a global must thus wrap it
in the macro <b><tt class="constant">COUNTER_G</tt></b>.
</p>
<div class="warning"><b class="warning">Warning</b>
<p class="simpara">
Any function which accesses globals must either be declared by Zend macros,
have <b><tt class="constant">TSRMLS_DC</tt></b> as its last argument, or call the macro
<b><tt class="constant">TSRMLS_FETCH</tt></b> before accessing the globals. See
the TSRM documentation for
more information.
</p>
</div>
<p class="simpara">
So with all of that in mind, here is our new version of the
<a href="internals2.counter.function.counter-get.php" class="function">counter_get()</a>:
</p>
<div class="example">
<p><b>Example #5 The right way to store the basic counter interface's value</b></p>
<div class="example-contents programlisting">
<div class="ccode"><pre class="ccode">/* php_counter.h */
ZEND_BEGIN_MODULE_GLOBALS(counter)
long basic_counter_value;
ZEND_END_MODULE_GLOBALS(counter)
#ifdef ZTS
#define COUNTER_G(v) TSRMG(counter_globals_id, zend_counter_globals *, v)
#else
#define COUNTER_G(v) (counter_globals.v)
#endif
/* counter.c */
ZEND_DECLARE_MODULE_GLOBALS(counter)
/* ... */
PHP_FUNCTION(counter_get)
{
RETURN_LONG(COUNTER_G(basic_counter_value));
}</pre>
</div>
</div>
</div>
<p class="simpara">
This is a correct implementation. It is not, however, a complete one. The
section <a href="internals2.structure.lifecycle.php" class="xref">Life cycle of an extension</a> explains why.
</p>
</div>
</div><?php manual_footer(); ?>