在 php 中创建超全局变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/834491/
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
create superglobal variables in php?
提问by Moon
is there a way to create my own custom superglobal variables like $_POST and $_GET?
有没有办法创建我自己的自定义超全局变量,如 $_POST 和 $_GET?
采纳答案by Scott Reynen
Static class variables can be referenced globally, e.g.:
静态类变量可以全局引用,例如:
class myGlobals {
static $myVariable;
}
function a() {
print myGlobals::$myVariable;
}
回答by Piero Mori
Yes, it is possible, but not with the so-called "core" PHP functionalities. You have to install an extension called runkit: http://www.php.net/manual/en/runkit.installation.php
是的,这是可能的,但不能使用所谓的“核心”PHP 功能。您必须安装一个名为 runkit 的扩展:http://www.php.net/manual/en/runkit.installation.php
After that, you can set your custom superglobals in php.ini as documented here: http://www.php.net/manual/en/runkit.configuration.php#ini.runkit.superglobal
之后,您可以在 php.ini 中设置自定义超全局变量,如下所示:http: //www.php.net/manual/en/runkit.configuration.php#ini.runkit.superglobal
回答by OneNerd
I think you already have it - every variable you create in global space can be accessed using the $GLOBALSsuberglobal like this:
我认为您已经拥有了 - 您在全局空间中创建的每个变量都可以使用$GLOBALSsuberglobal访问,如下所示:
// in global space
$myVar = "hello";
// inside a function
function foo() {
echo $GLOBALS['myVar'];
}
回答by IntelliAdmin
One other way to get around this issue is to use a static class method or variable.
解决此问题的另一种方法是使用静态类方法或变量。
For example:
例如:
class myGlobals {
public static $myVariable;
}
Then, in your functions you can simply refer to your global variable like this:
然后,在您的函数中,您可以像这样简单地引用全局变量:
function Test()
{
echo myGlobals::$myVariable;
}
Not as clean as some other languages, but at least you don't have to keep declaring it global all the time.
不像其他一些语言那么干净,但至少你不必一直声明它是全局的。
回答by Guntarss
Class Registry {
private $vars = array();
public function __set($index, $value){$this->vars[$index] = $value;}
public function __get($index){return $this->vars[$index];}
}
$registry = new Registry;
function _REGISTRY(){
global $registry;
return $registry;
}
_REGISTRY()->sampleArray=array(1,2,'red','white');
//_REGISTRY()->someOtherClassName = new className;
//_REGISTRY()->someOtherClassName->dosomething();
class sampleClass {
public function sampleMethod(){
print_r(_REGISTRY()->sampleArray); echo '<br/>';
_REGISTRY()->sampleVar='value';
echo _REGISTRY()->sampleVar.'<br/>';
}
}
$whatever = new sampleClass;
$whatever->sampleMethod();
回答by Geo
回答by Rik Heywood
Not really. though you can just abuse the ones that are there if you don't mind the ugliness of it.
并不真地。虽然如果你不介意它的丑陋,你可以滥用那里的那些。
回答by Ian Warner
You can also use the Environment variables of the server, and access these in PHP This is a good way to maybe store global database access if you own and exclusively use the server.
您还可以使用服务器的环境变量,并在 PHP 中访问这些变量。如果您拥有并专门使用服务器,这是一种可能存储全局数据库访问权限的好方法。
回答by T.Todua
possible workaround with $GLOBALS:
可能的解决方法$GLOBALS:
file.php:
文件.php:
$GLOBALS['xyz'] = "hello";
any_included_file.php:
any_included_file.php:
echo $GLOBALS['xyz'];

