在class的function中include的文件中,非php的global全局环境
测试代码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
<?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 => ', $g1 ,'$g2 => ', $g2 ,'$gg2 => ', $gg2); echo "\n--- ----\n";
代码2.php
<?php $g2 = 'g2'; global $gg2;//本环境下并非全局,需要提升 $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";
结果
--- 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" --- ----
由此可见,
在class中include后,被include文件变量域已经变成func中了,非全局.
但是可以通过global提升.
一般被include文件在编写时,可能会由于没有注意到被include的情况,就觉得有点郁闷了.

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

CakePHP 是 PHP 的开源框架。它的目的是使应用程序的开发、部署和维护变得更加容易。 CakePHP 基于类似 MVC 的架构,功能强大且易于掌握。模型、视图和控制器 gu

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写
