cvs: phpdoc /en/language variables.xml

From: Date: Sun, 23 Jun 2002 03:03:38 +0000
Subject: cvs: phpdoc /en/language variables.xml
Groups: php.doc 
Request: Send a blank email to phpdoc+get-969346920@lists.php.net to get a copy of this message
david Sat Jun 22 23:03:38 2002 EDT Modified files: /phpdoc/en/language variables.xml Log: notes about static initializers Index: phpdoc/en/language/variables.xml diff -u phpdoc/en/language/variables.xml:1.49 phpdoc/en/language/variables.xml:1.50 --- phpdoc/en/language/variables.xml:1.49 Sun Jun 16 03:11:01 2002 +++ phpdoc/en/language/variables.xml Sat Jun 22 23:03:36 2002 @@ -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> @@ -454,6 +454,54 @@ Test (); } $count--; +} +]]> + </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>

« previous php.doc (#969346920) next »