【发布时间】:2016-11-19 21:24:19
【问题描述】:
我在网上浏览了一个关于 web 应用程序属性的管理器 PHP 变量的示例。 主要问题是我不喜欢写这个代码:
我想在文件中包含一个 .php 类;加上变量列表,如下所示:
$MSGdisplay = '';
$MSGemail = '';
$MSGnotification = '';
并通过简单的调用在脚本中的任何位置使用它们:
$G['MSGdisplay'] = 'This is an example of code'; //more short that $_GLOBALS array
不会失去分配新值的能力。
示例:
文件索引.php
<?php
require_once("main.php");
global $G;
$G['test'] = 'Text Test';
$WebAPP = new Class_MAIN();
$WebAPP -> Main();
?>
文件 Main.php
<?php
class Class_MAIN{
function Main(){
echo $G['test'];
}
}
?>
注意:未定义变量:第 4 行 main.php 中的 G
【问题讨论】:
-
在require_once之前定义全局变量
-
全球 $G; require_once("main.php"); $G['test'] = '文本测试';不工作同样的问题
标签: php variables properties