Home > Article > Backend Development > How to correctly understand PHP include scope_PHP Tutorial
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>
<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>
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>
<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>
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