📄 language.variables.scope.html
字号:
</p> <p class="simpara"> This function is quite useless since every time it is called it sets <var class="varname">$a</var> to <i>0</i> and prints "0". The <var class="varname">$a</var>++ which increments the variable serves no purpose since as soon as the function exits the <var class="varname">$a</var> variable disappears. To make a useful counting function which will not lose track of the current count, the <var class="varname">$a</var> variable is declared static: </p> <p class="para"> <div class="example"> <p><b>Example #5 Example use of static variables</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">Test</span><span style="color: #007700">()<br />{<br /> static </span><span style="color: #0000BB">$a </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /> echo </span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$a</span><span style="color: #007700">++;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="simpara"> Now, every time the Test() function is called it will print the value of <var class="varname">$a</var> and increment it. </p> <p class="simpara"> Static variables also provide one way to deal with recursive functions. A recursive function is one which calls itself. Care must be taken when writing a recursive function because it is possible to make it recurse indefinitely. You must make sure you have an adequate way of terminating the recursion. The following simple function recursively counts to 10, using the static variable <var class="varname">$count</var> to know when to stop: </p> <p class="para"> <div class="example"> <p><b>Example #6 Static variables with recursive functions</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">Test</span><span style="color: #007700">()<br />{<br /> static </span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /><br /> </span><span style="color: #0000BB">$count</span><span style="color: #007700">++;<br /> echo </span><span style="color: #0000BB">$count</span><span style="color: #007700">;<br /> if (</span><span style="color: #0000BB">$count </span><span style="color: #007700">< </span><span style="color: #0000BB">10</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">Test</span><span style="color: #007700">();<br /> }<br /> </span><span style="color: #0000BB">$count</span><span style="color: #007700">--;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <blockquote><p><b class="note">Note</b>: Static variables may be declared as seen in the examples above. Trying to assign values to these variables which are the result of expressions will cause a parse error. <br /> <div class="example"> <p><b>Example #7 Declaring static variables</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">foo</span><span style="color: #007700">(){<br /> static </span><span style="color: #0000BB">$int </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #FF8000">// correct <br /> </span><span style="color: #007700">static </span><span style="color: #0000BB">$int </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">+</span><span style="color: #0000BB">2</span><span style="color: #007700">; </span><span style="color: #FF8000">// wrong (as it is an expression)<br /> </span><span style="color: #007700">static </span><span style="color: #0000BB">$int </span><span style="color: #007700">= </span><span style="color: #0000BB">sqrt</span><span style="color: #007700">(</span><span style="color: #0000BB">121</span><span style="color: #007700">); </span><span style="color: #FF8000">// wrong (as it is an expression too)<br /><br /> </span><span style="color: #0000BB">$int</span><span style="color: #007700">++;<br /> echo </span><span style="color: #0000BB">$int</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <br /> </p></blockquote> </div> <div id="language.variables.scope.references" class="sect2"> <h3 class="title">References with global and static variables</h3> <p class="simpara"> The Zend Engine 1, driving PHP 4, implements the <a href="language.variables.scope.html#language.variables.scope.static" class="link">static</a> and <a href="language.variables.scope.html#language.variables.scope.global" class="link">global</a> modifier for variables in terms of <a href="language.references.html" class="link"> references</a>. For example, a true global variable imported inside a function scope with the <i>global</i> statement actually creates a reference to the global variable. This can lead to unexpected behaviour which the following example addresses: </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">test_global_ref</span><span style="color: #007700">() {<br /> global </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$obj </span><span style="color: #007700">= &new </span><span style="color: #0000BB">stdclass</span><span style="color: #007700">;<br />}<br /><br />function </span><span style="color: #0000BB">test_global_noref</span><span style="color: #007700">() {<br /> global </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">stdclass</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">test_global_ref</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">test_global_noref</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="simpara"> Executing this example will result in the following output: </p> <div class="example-contents"><pre>NULLobject(stdClass)(0) {} </pre></div> <p class="simpara"> A similar behaviour applies to the <i>static</i> statement. References are not stored statically: </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function &</span><span style="color: #0000BB">get_instance_ref</span><span style="color: #007700">() {<br /> static </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br /><br /> echo </span><span style="color: #DD0000">'Static object: '</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /> if (!isset(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// Assign a reference to the static variable<br /> </span><span style="color: #0000BB">$obj </span><span style="color: #007700">= &new </span><span style="color: #0000BB">stdclass</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">property</span><span style="color: #007700">++;<br /> return </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br />}<br /><br />function &</span><span style="color: #0000BB">get_instance_noref</span><span style="color: #007700">() {<br /> static </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br /><br /> echo </span><span style="color: #DD0000">'Static object: '</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /> if (!isset(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">)) {<br /> </span><span style="color: #FF8000">// Assign the object to the static variable<br /> </span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">stdclass</span><span style="color: #007700">;<br /> }<br /> </span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">property</span><span style="color: #007700">++;<br /> return </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$obj1 </span><span style="color: #007700">= </span><span style="color: #0000BB">get_instance_ref</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$still_obj1 </span><span style="color: #007700">= </span><span style="color: #0000BB">get_instance_ref</span><span style="color: #007700">();<br />echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$obj2 </span><span style="color: #007700">= </span><span style="color: #0000BB">get_instance_noref</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$still_obj2 </span><span style="color: #007700">= </span><span style="color: #0000BB">get_instance_noref</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="simpara"> Executing this example will result in the following output: </p> <div class="example-contents"><pre>Static object: NULLStatic object: NULLStatic object: NULLStatic object: object(stdClass)(1) { ["property"]=> int(1)} </pre></div> <p class="simpara"> This example demonstrates that when assigning a reference to a static variable, it's not <em class="emphasis">remembered</em> when you call the <i>&get_instance_ref()</i> function a second time. </p> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.variables.predefined.html">Predefined Variables</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.variables.variable.html">Variable variables</a></div> <div class="up"><a href="language.variables.html">Variables</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -