php 使变量可用于所有类、方法、函数和包含,就像 $_POST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15444952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
make variable available to all classes, methods, functions and includes, just like $_POST
提问by olli
This question seems simple enough, but I can't find an answer anywhere...
这个问题看起来很简单,但我在任何地方都找不到答案......
At the beginning of my php script/file I want to create a variable.
在我的 php 脚本/文件的开头,我想创建一个变量。
$variable = 'this is my variable';
I want this variable to be available within the entire script, so that all classes, methods, functions, included scripts, etc. can simple call this variable as $variable.
我希望这个变量在整个脚本中可用,以便所有类、方法、函数、包含的脚本等都可以简单地将此变量称为 $variable。
Class MyClass
{
public function showvariable()
{
echo $variable;
}
}
The $_SESSION, $_POST, $_GET variables all behave like that. I can just write a method and use $_POST and it'll work. but if I use $variable, which I have defined at the beginning of my script, it says "Notice: Undefined variable: variable in ..." It even says it, when I write a function, rather than a class method...
$_SESSION、$_POST、$_GET 变量的行为都是如此。我可以编写一个方法并使用 $_POST 并且它会起作用。但是,如果我使用在脚本开头定义的 $variable,它会显示“注意:未定义变量:变量 in ...”,当我编写函数而不是类方法时,它甚至会这样说。 .
I tried to write
我试着写
global $variable = 'this is my variable';
But then the script wouldn't load... "HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request."
但是脚本不会加载...“HTTP 错误 500(内部服务器错误):服务器尝试完成请求时遇到意外情况。”
How can I make a variable truly globally accessible like $_POST?
如何使变量像 $_POST 一样真正可以全局访问?
Why would I need this? I specifically plan on using it as a form token. At the top of the page I generate my form token as $token, at the bottom of the page I store it in the session. Before handling any form I check if the SESSION token matches the POST token... And since I often have multiple forms on the page (login form in the top nav, and register form in the main body) i just wanna make it convenient and call $token on each form. Sometimes my formelements are generated by classes or functions, but they don't recognize the variable and say it's not defined.
为什么我需要这个?我特别计划将它用作表单标记。在页面顶部,我将表单令牌生成为 $token,在页面底部,我将其存储在会话中。在处理任何表单之前,我会检查 SESSION 令牌是否与 POST 令牌匹配......而且由于我经常在页面上有多个表单(顶部导航中的登录表单,以及主体中的注册表单),我只是想让它方便并且在每个表单上调用 $token。有时我的表单元素是由类或函数生成的,但它们不识别变量并说它没有定义。
采纳答案by olli
I found it... -.- I thought it would be easy enough...
我找到了...... -.- 我认为这很容易......
I have to call to the variable by using
我必须通过使用调用变量
$GLOBALS['variable'];
Just found it... finally... http://php.net/manual/en/reserved.variables.globals.php
刚刚找到它......终于...... http://php.net/manual/en/reserved.variables.globals.php
EDITlike 8 months later or so:
大约8 个月后编辑:
I just learned about CONSTANTS!
我刚刚了解了常量!
define('name', 'value');
They just can't be reassigned... I guess I could use that, too! http://www.php.net/manual/en/language.constants.php
它们不能被重新分配......我想我也可以使用它! http://www.php.net/manual/en/language.constants.php
回答by Buttle Butkus
Just define a variable outside of any class or function. Then use keyword globalinside any class or function.
只需在任何类或函数之外定义一个变量。然后global在任何类或函数中使用关键字。
<?php
// start of code
$my_var = 'Hellow Orld';
function myfunc() {
global $my_var;
echo $my_var; // echoes 'Hellow Orld';
}
function myOtherFunc() {
var $my_var;
echo $my_var; // echoes nothing, because $my_var is an undefined local variable.
}
class myClass {
public function myFunc() {
global $my_var;
echo $my_var; // echoes 'Hellow Orld';
}
public function myOtherFunc() {
var $my_var;
echo $my_var; // echoes nothing.
}
}
myFunc(); // "Hellow Orld"
myOtherFunc(); // no output
myClass->myFunc(); // "Hellow Orld"
myClass->myOtherFunc(); // no output
// end of file
?>
回答by kennypu
you need to declare global to access it in any function scope:
您需要声明 global 才能在任何函数范围内访问它:
at the top of your script:
在脚本的顶部:
global $var;
$var= "this is my variable";
in your class
在你的班级
...
public function showvariable(){
global $var;
echo $var;
}
...
回答by erlandsson
In PHP, global variables must be declared as such within each function in which they will be used. See: PHP Manual, section on variable scope
在 PHP 中,必须在每个将使用它们的函数中声明全局变量。请参阅:PHP 手册,关于变量作用域的部分
That being said, global variables are often a bad idea, and in most cases there's a way to avoid their use.
话虽如此,全局变量通常是一个坏主意,在大多数情况下,有一种方法可以避免使用它们。

