Real-world Global environment _php techniques for testing in files contained in class's function include non-PHP

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.