Home > Backend Development > PHP Tutorial > How to correctly understand PHP include scope_PHP Tutorial

How to correctly understand PHP include scope_PHP Tutorial

WBOY
Release: 2016-07-15 13:34:11
Original
785 people have browsed it

Many programmers are using

Note: This document is based on the include statement, but it also applies to require. The two constructs are identical except for how they handle include failure: include() generates a warning and continues execution when an include fails, while require() results in a fatal error. In other words, use require() if you want to stop processing the page when a missing file is encountered, otherwise use include().

PHP include scope 1. c

<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>$</span><span class="attribute">color</span><span> = </span><span class="attribute-value">'green'</span><span>;  </span></li><li class="alt"><span>$</span><span class="attribute">fruit</span><span> = </span><span class="attribute-value">'apple'</span><span>;  </span></li><li><span class="tag">?></span><span> </span></span></li></ol>
Copy after login
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>function foo()  </span></li><li class="alt"><span>{  </span></li><li><span>global $color;  </span></li><li class="alt"><span>include 'vars.php';  </span></li><li><span>echo "A $color $fruit";  </span></li><li class="alt"><span>}  </span></li><li><span>foo();   </span></li><li class="alt"><span>// A green apple  </span></li><li><span>echo "A $color $fruit";   </span></li><li class="alt"><span>// A green  </span></li><li><span class="tag">?></span><span>  </span></span></li></ol>
Copy after login

As can be seen from this example:

(1) The PHP include scope of the variable in the included file follows the scope of the including file. That is, use include in a function to include variables from other files, and the scope of these variables is within the function.

(2) The value of $color can be printed out outside the foo() function, which does not violate the provisions of (1). That's because $color has been declared as global at the beginning of the function (although there is no $color variable outside the foo() function, the $color variable at this time is not the $color variable in vars.php, but a mandatory declaration as "global" A new variable, it has not been assigned a value at this time. When the following is included in vars.php, according to the principle of (1), the $color variable in vars.php automatically enjoys the scope within the function, so its value is global The value of variable $color)

PHP include scope 2. Function and class scopes

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>class ClassB {  </span></li><li class="alt"><span>/**  </span></li><li><span>* constructor  </span></li><li class="alt"><span>*/  </span></li><li><span>public function __construct(){}  </span></li><li class="alt"><span>/**  </span></li><li><span>* destructor  </span></li><li class="alt"><span>*/  </span></li><li><span>public function __destruct() {}  </span></li><li class="alt"><span>public function printit() {  </span></li><li><span>echo 'print it in ClassB.</span><span class="tag"><</span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>';  </span></span></li>
<li class="alt"><span>}   </span></li>
<li><span>}  </span></li>
<li class="alt"><span>function show_func_included() {  </span></li>
<li>
<span>echo 'show_func_included</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span>';  </span>
</li>
<li class="alt"><span>}  </span></li>
<li>
<span class="tag">?></span><span> </span>
</li>
</ol>
Copy after login
<ol class="dp-xml">
<li class="alt"><span><span class="tag"><</span><span> ?php  </span></span></li><li><span>function include_class() {  </span></li><li class="alt"><span>include('classb.php');  </span></li><li><span>}  </span></li><li class="alt"><span>include_class();  </span></li><li><span>$</span><span class="attribute">objB</span><span> = </span><span class="attribute-value">new</span><span> ClassB();  </span></li><li class="alt"><span>$objB-</span><span class="tag">></span><span>printit();   </span></span></li>
<li><span>// print it in ClassB.  </span></li>
<li class="alt"><span>show_func_included()   </span></li>
<li><span>// show_func_included  </span></li>
<li class="alt">
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copy after login

As you can see from this example Exit :

After being included, all functions and classes defined in the included file have global scope in the included file

Conclusion:

1. The PHP include scope of the variable in the included file follows (does not change) the scope where the included file is located.
2. All functions and classes defined in the included file will have global scope in the included file after being included


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446001.htmlTechArticleMany programmers are using Note: This document is based on the include statement, but it also applies to require. The two structures are exactly the same except for how to handle include failure: on include failure...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template