设置全局数组(类外)
我有几个类(在单独的文件中,例如/classes/admin.class.php),我希望它们能够访问全局数组,该数组加载在主文件(index.php)中。
我希望全局数组的内容为 $cq->fetch_assoc()
。
我该怎么做?我已经读过相关内容,但我根本无法理解它。
$GLOBALS
似乎可以工作,但这不是过时的版本吗(例如 $HTTP_POST_ARRAY
?)
提前致谢
I have a few classes (in separate files, eg /classes/admin.class.php), and I'd like them to have access to a global array, which is loaded in the main file (index.php).
I want the contents of the global array to be $cq->fetch_assoc()
.
How do I do this? I've read up about it, but I simply cannot get my head around it.
$GLOBALS
seems to work, but is that not an outdated version (like $HTTP_POST_ARRAY
?)
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在index.php 文件中声明的任何变量都将可用于所有后续包含的文件。但是,一旦进入函数定义,该变量将不可用。您可以使用“global”关键字使其可用。
例如:
index.php
/classes/admin.inc.php
Any variable you declare in the index.php file will be available to all the subsequently included files. However, once you go into a function definition, that variable won't be available. You can make it available by using the "global" keyword.
For example:
index.php
/classes/admin.inc.php
不,这是两个不同的东西。
$GLOBALS
并没有过时,而是超全局变量,它使您可以访问 PHP 中的全局变量表。$HTTP_POST_ARRAY
已过时,因为您应该使用$_POST
代替。No, that are two different things.
$GLOBALS
is not outdated, but the superglobal variable that gives you access to the global variable table in PHP.$HTTP_POST_ARRAY
is outdated, because you should use$_POST
instead.