notes about static initializers

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@86453 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
David Croft 2002-06-23 03:03:36 +00:00
parent 6173c244f9
commit a4489470e1

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.49 $ -->
<!-- $Revision: 1.50 $ -->
<chapter id="language.variables">
<title>Variables</title>
@ -459,6 +459,54 @@ function Test()
</programlisting>
</informalexample>
<simpara>
Static initializers are evaluated when a function is defined, not
when it is executed. Thus you cannot make reference to any other
variables or initialize a static variable with the result of a
function call. The following example suggests a way around this:
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
function Test()
{
static $magic_quotes_initialized = false;
static $magic_quotes;
if (!$magic_quotes_initialized) {
$magic_quotes = get_magic_quotes_gpc();
$magic_quotes_initialized = true;
}
// function body
}
]]>
</programlisting>
</informalexample>
<simpara>
If the function you are calling has predictable return values, for
example, if it can never return <literal>null</literal>, this
can be further simplified (note the use of the identity operator
<literal>===</literal>):
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
function Test()
{
static $magic_quotes = null;
if ($magic_quotes === null) {
$magic_quotes = get_magic_quotes_gpc();
}
// function body
}
]]>
</programlisting>
</informalexample>
<simpara>
The Zend Engine 1, driving <literal>PHP4</literal>, implements the
<literal>static</literal> and <literal>global</literal> modifier for