Actual measurement of the non-php global global environment in the included file in the class function_PHP tutorial

WBOY
Release: 2016-07-21 15:00:44
Original
770 people have browsed it

Test code 1.php

Copy code The code is as follows:

$g1 = 'g1' ;
class c{
function fun() {
include('2.php');
echo "n-----in class fun---n";
global $g1;
var_dump("$g1 => ", $g1
,'$g2 => ', $g2
,'$gg2 => ', $gg2
);
echo "n--------n";
}
}
c::fun();
echo "n--- in 1.php ----n";
var_dump('$g1 => ', $g1
,'$g2 => ', $g2
,'$gg2 => ', $gg2 );
echo "n--- ----n";

code2.php
Copy code The code is as follows:

$g2 = 'g2';
global $gg2;//This environment is not global and needs to be improved
$gg2 = 'gg2';
function g2fun() {
global $g1, $g2, $gg2;
echo "n--- in g2fun ----n";
var_dump('$ g1 => ', $g1, '$g2 => ', $g2
, '$gg2 => ', $gg2);
echo "n--- ----n" ;
}
g2fun();
echo "n--- in 2.php ----n";
var_dump('$g1 => ', $g1, '$ g2 => ', $g2
, '$gg2 => ', $gg2
);
echo "n--- ----n";
global $g1;
echo "n--- in 2.php global----n";
var_dump('$g1 => ', $g1, '$g2 => ', $g2
, '$gg2 => ', $gg2
);
echo "n--- ----n";

result
Copy code The code is as follows:

--- in g2fun ----
string(7) "$g1 => "
string(2) "g1"
string(7) "$g2 => "
NULL
string(8) "$gg2 => "
string(3) "gg2"
--- ----
--- in 2.php ----
string(7) "$g1 => "
NULL
string(7) "$ g2 => "
string(2) "g2"
string(8) "$gg2 => "
string(3) "gg2"
--- ----
--- in 2.php global----
string(7) "$g1 => "
string(2) "g1"
string(7) "$g2 => ; "
string(2) "g2"
string(8) "$gg2 => "
string(3) "gg2"
--- ----
- ----in class fun---
string(7) "$g1 => "
string(2) "g1"
string(7) "$g2 => "
string(2) "g2"
string(8) "$gg2 => "
string(3) "gg2"
--------
--- in 1.php ----
string(7) "$g1 => "
string(2) "g1"
string(7) "$g2 => "
NULL
string(8) "$gg2 => "
string(3) "gg2"
--- ----

It can be seen that
is in After being included in the class, the variable domain of the included file has become in func, non-global.
But it can be improved through global.
Generally, when the included file is written, it may be because the included file is not noticed. , I felt a little depressed.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328040.htmlTechArticleTest code 1.php Copy the code as follows: ?php $g1 = 'g1'; class c{ function fun () { include('2.php'); echo "n-----in class fun---n"; global $g1; var_dump("$g1 = ", $g1 ,'$g2...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!