正如標題所言經測試的結果為
在class中include後
被include文件變量域已經變成func中了
非全局
但是可以通過global提升
感興趣的朋友可以參考下哈
希望對大家有所幫助
測試代碼php
復制代碼 代碼如下:
<?php
$g
=
g
;
class c{
function fun() {
include(
php
);
echo "n
in class fun
n";
global $g
;
var_dump("$g
=> "
$g
$g
=>
$g
$gg
=>
$gg
);
echo "n
n";
}
}
c::fun();
echo "n
in
php
n";
var_dump(
$g
=>
$g
$g
=>
$g
$gg
=>
$gg
);
echo "n
n";
代碼php
復制代碼 代碼如下:
<?php
$g
=
g
;
global $gg
;//本環境下並非全局
需要提升
$gg
=
gg
;
function g
fun() {
global $g
$g
$gg
;
echo "n
in g
fun
n";
var_dump(
$g
=>
$g
$g
=>
$g
$gg
=>
$gg
);
echo "n
n";
}
g
fun();
echo "n
in
php
n";
var_dump(
$g
=>
$g
$g
=>
$g
$gg
=>
$gg
);
echo "n
n";
global $g
;
echo "n
in
php global
n";
var_dump(
$g
=>
$g
$g
=>
$g
$gg
=>
$gg
);
echo "n
n";
結果
復制代碼 代碼如下:
in g
fun
string(
) "$g
=> "
string(
) "g
"
string(
) "$g
=> "
NULL
string(
) "$gg
=> "
string(
) "gg
"
in
php
string(
) "$g
=> "
NULL
string(
) "$g
=> "
string(
) "g
"
string(
) "$gg
=> "
string(
) "gg
"
in
php global
string(
) "$g
=> "
string(
) "g
"
string(
) "$g
=> "
string(
) "g
"
string(
) "$gg
=> "
string(
) "gg
"
in class fun
string(
) "$g
=> "
string(
) "g
"
string(
) "$g
=> "
string(
) "g
"
string(
) "$gg
=> "
string(
) "gg
"
in
php
string(
) "$g
=> "
string(
) "g
"
string(
) "$g
=> "
NULL
string(
) "$gg
=> "
string(
) "gg
"
由此可見
在class中include後被include文件變量域已經變成func中了非全局
但是可以通過global提升
一般被include文件在編寫時可能會由於沒有注意到被include的情況就覺得有點郁悶了
From:http://tw.wingwit.com/Article/program/PHP/201311/21095.html