Home > php教程 > php手册 > body text

在class的function中include的文件中,非php的global全局环境

WBOY
Release: 2016-06-06 19:49:20
Original
1595 people have browsed it

测试代码1.php ?php$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 = ',

测试代码1.php

 ", $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";
Copy after login

代码2.php
 ', $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";
Copy after login

结果
--- 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"

--- ----
Copy after login


由此可见,

在class中include后,被include文件变量域已经变成func中了,非全局.

但是可以通过global提升.


一般被include文件在编写时,可能会由于没有注意到被include的情况,就觉得有点郁闷了.


Related labels:
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!