Rumah >pembangunan bahagian belakang >tutorial php >如何正确理解PHP include作用域_PHP教程
很多程序员在使用
注意:本文档基于include叙述,但也适用于 require。这两种结构除了在如何处理包含失败之外,其他完全一样:在包含失败时,include()产生一个警告并继续执行,而require()则导致一个致命错误。换句话说,如果想在遇到丢失文件时停止处理页面就用require(),否则就用include()。
PHP include作用域1、c
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></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> </li> </ol>
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></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> </li> </ol>
由此例可看出:
(1) 被包含文件的变量的PHP include作用域遵从包含文件所在处的作用域。即在函数里使用include将其他文件的变量包含进来,这些变量的作用域为该函数内。
(2) foo()函数外能打印出$color的值,并没有违反(1)的规定。那是因为函数开始已经声明$color为global (尽管foo()函数外并没有$color变量,此时的$color变量并不是vars.php里面的$color变量,而是一个强制声明为“全局”的新变量,这时它还没有被赋值,当下面包含进vars.php后,根据(1)的原则,vars.php中的$color变量自动享有函数内的作用域,所以它的值就是全局变量$color的值)
PHP include作用域2、函数、类的作用域
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><span> ?php </span></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 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 class="tag-name">br</span><span class="tag">/></span><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> ?php </span></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> </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>
由此例可以看出:
所有在被包含文件中定义的函数和类在被包含后,在包含文件里都具有全局作用域
结论:
1、被包含文件的变量的PHP include作用域遵从(不改变)包含文件所在处的作用域。
2、所有在被包含文件中定义的函数和类在被包含后,在包含文件里都具有全局作用域