我在全局范围内有一个名为${SYSTEM}
的变量,其中SYSTEM是一个已定义的常量。我有很多类需要访问这个变量的函数,我发现每次声明global ${SYSTEM};
都很烦人。
我尝试声明一个类变量:public ${SYSTEM} = $GLOBALS[SYSTEM];
但这会导致语法错误,这很奇怪,因为我有另一个以这种方式声明类变量的类,似乎工作正常。我唯一能想到的是这个常数没有得到认可。
我已经设法用一个构造函数来解决这个问题但我正在寻找一个更简单的解决方案。
修改 全局$ {SYSTEM}变量是一个包含许多其他子数组的数组。不幸的是,似乎没有办法绕过使用构造函数......
答案 0 :(得分:8)
好的,希望我已经掌握了你想要实现的目标
<?php
// the global array you want to access
$GLOBALS['uname'] = array('kernel-name' => 'Linux', 'kernel-release' => '2.6.27-11-generic', 'machine' => 'i686');
// the defined constant used to reference the global var
define(_SYSTEM_, 'uname');
class Foo {
// a method where you'd liked to access the global var
public function bar() {
print_r($this->{_SYSTEM_});
}
// the magic happens here using php5 overloading
public function __get($d) {
return $GLOBALS[$d];
}
}
$foo = new Foo;
$foo->bar();
?>
答案 1 :(得分:8)
这是我在没有全局的情况下全局访问的方式。
class exampleGetInstance
{
private static $instance;
public $value1;
public $value2;
private function initialize()
{
$this->value1 = 'test value';
$this->value2 = 'test value2';
}
public function getInstance()
{
if (!isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class();
self::$instance->initialize();
}
return self::$instance;
}
}
$myInstance = exampleGetInstance::getInstance();
echo $myInstance->value1;
$myInstance
现在是对exampleGetInstance
class。
修正了格式
答案 2 :(得分:2)
你可以使用这样的构造函数:
class Myclass {
public $classvar;
function Myclass() {
$this->classvar = $GLOBALS[SYSTEM];
}
}
编辑:谢谢你指出错字,彼得!
这也适用于数组。如果不需要分配,则参考也可以:
$this->classvar =& $GLOBALS[SYSTEM];
EDIT2:以下代码用于测试此方法,它在我的系统上运行:
<?php
define('MYCONST', 'varname');
$varname = array("This is varname", "and array?");
class Myclass {
public $classvar;
function Myclass() {
$this->classvar =& $GLOBALS[MYCONST];
}
function printvar() {
echo $this->classvar[0];
echo $this->classvar[1];
}
};
$myobj = new Myclass;
$myobj->printvar();
?>
答案 3 :(得分:1)
成员变量的直接规范不能包含对其他变量的任何引用(class {public $membervar = $outsidevar;}
也是无效的)。改为使用构造函数。
但是,当您处理常量时,为什么不使用php的constant或class constant设施?
答案 4 :(得分:1)
你正试图在这里做一些非常不寻常的事情,所以你可能会觉得它很尴尬。使用全局变量永远不会令人愉快,尤其是使用SYSTEM
常量选择动态名称时。我个人建议您到处使用$GLOBALS[SYSTEM]
,或者......
$sys = $GLOBALS[SYSTEM];
...如果你打算多用它。
答案 5 :(得分:-1)
你也可以尝试单例模式,虽然在某种程度上它在OOP圈子中是不受欢迎的,但它通常被称为类的全局变量。
<?php
class Singleton {
// object instance
private static $instance;
// The protected construct prevents instantiating the class externally. The construct can be
// empty, or it can contain additional instructions...
protected function __construct() {
...
}
// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects. The methods can be empty, or
// can contain additional code (most probably generating error messages in response
// to attempts to call).
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}
//This method must be static, and must return an instance of the object if the object
//does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
//One or more public methods that grant access to the Singleton object, and its private
//methods and properties via accessor methods.
public function GetSystemVar() {
...
}
}
//usage
Singleton::getInstance()->GetSystemVar();
?>
这个例子稍微从维基百科修改,但你可以得到这个想法。尝试谷歌搜索单例模式以获取更多信息
答案 6 :(得分:-2)
我要说的是前两个让我感到高兴的事情:
这应该是你要找的东西
class SomeClass {
public $system = $GLOBALS['system'];
}
您也可以使用类常量,而不是
class SomeClass {
const SYSTEM = $GLOBALS['system'];
}
这可以在类中使用'self :: SYSTEM'引用,在外部引用'SomeClass :: SYSTEM'。