Test Code 1.php
Copy Code code as follows:
<?php
$g 1 = ' G1 ';
Class c{
function Fun () {
Include (' 2.php ');
echo "\ n-----in class fun---\ n";
Global $g 1;
Var_dump ("\ $g 1 =>", $g 1
, ' $g 2 => ', $g 2
, ' $gg 2 => ', $gg 2
);
echo "\ n--------\ n";
}
}
C::fun ();
echo "\ n---in 1.php----\ n";
Var_dump (' $g 1 => ', $g 1
, ' $g 2 => ', $g 2
, ' $gg 2 => ', $gg 2);
echo "\ n-------\ n";
Code 2.php
Copy Code code as follows:
<?php
$g 2 = ' G2 ';
Global $GG 2;//This environment is not the whole, need to upgrade
$gg 2 = ' gg2 ';
function G2fun () {
Global $g 1, $g 2, $gg 2;
echo "\ n---in g2fun----\ n";
Var_dump (' $g 1 => ', $g 1, ' $g 2 => ', $g 2
, ' $gg 2 => ', $gg 2);
echo "\ n-------\ n";
}
G2fun ();
echo "\ n---in 2.php----\ n";
Var_dump (' $g 1 => ', $g 1, ' $g 2 => ', $g 2
, ' $gg 2 => ', $gg 2
);
echo "\ n-------\ n";
Global $g 1;
echo "\ n---in 2.php global----\ n";
Var_dump (' $g 1 => ', $g 1, ' $g 2 => ', $g 2
, ' $gg 2 => ', $gg 2
);
echo "\ n-------\ n";
Results
Copy Code code as follows:
---in g2fun----
String (7) "$g 1 =>"
String (2) "G1"
Stri Ng (7) "$g 2 =>"
NULL
String (8) "$gg 2 =>"
String (3) "GG2"
-------
---in 2.php----
String (7) "$g 1 =>"
NULL
String (7) "$g 2 =>"
String (2) "G2"
String (8) "$gg 2 =>" Br>string (3) "GG2"
-------
---in 2.php global----
String (7) "$g 1 =>"
String (2) "G1"
St Ring (7) "$g 2 =>"
String (2) "G2"
String (8) "$gg 2 =>"
String (3) "GG2"
-------
-----in Class Fun---
string (7) "$g 1 =>"
String (2) "G1"
String (7) "$g 2 =>"
String (2) "G2"
St Ring (8) "$gg 2 =>"
String (3) "Gg2"
--------
---in 1.php----
String (7) "$g 1 =>"
Stri Ng (2) "G1"
String (7) "$g 2 =>"
NULL
String (8) "$gg 2 =>"
String (3) "GG2"
-------
Thus
After include in class, the Include file Variable field has become Func, not global.
But it can be promoted through global.
Typically, an include file may feel a bit depressing when it is written, because it doesn't notice the include.