13

我在名为 的全局范围内有一个变量${SYSTEM},其中 SYSTEM 是一个已定义的常量。我有很多类的函数需要访问这个变量,我发现global ${SYSTEM};每次都声明很烦人。

我尝试声明一个类变量:public ${SYSTEM} = $GLOBALS[SYSTEM];但这会导致语法错误,这很奇怪,因为我有另一个类以这种方式声明类变量并且似乎工作正常。我唯一能想到的是常数没有被识别。

我已经设法通过构造函数来解决这个问题,但在求助之前我正在寻找一个更简单的解决方案。


编辑 全局 ${SYSTEM} 变量是一个包含许多其他子数组的数组。不幸的是,似乎没有办法使用构造函数来解决......

4

7 回答 7

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类实例的引用。

固定格式

于 2009-02-06T13:13:21.783 回答
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();

?>
于 2009-02-06T07:12:27.053 回答
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();
?>
于 2009-02-06T04:49:42.300 回答
1

成员变量的直接说明不能包含对其他变量的任何引用(class {public $membervar = $outsidevar;}也是无效的)。请改用构造函数。

但是,当您处理常量时,为什么不使用 php 的常量类常量工具呢?

于 2009-02-06T04:22:50.000 回答
1

您正在尝试在这里做一些非常不寻常的事情,因此您可以预期它会很尴尬。SYSTEM使用全局变量从来都不是一件令人愉快的事情,尤其是在使用常量选择动态名称时。就个人而言,我建议您$GLOBALS[SYSTEM]在任何地方使用,或者...

$sys = $GLOBALS[SYSTEM];

...如果您要经常使用它。

于 2009-02-06T05:09:37.220 回答
-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();

?>

这个例子是从维基百科稍微修改的,但你可以理解。尝试使用谷歌搜索单例模式以获取更多信息

于 2009-02-06T04:32:46.577 回答
-2

我想说的前两件事对我来说很突出:

  1. 你不需要变量名的括号,你可以简单地做 public $system 或 public $SYSTEM。
  2. 虽然 PHP 可能并不总是需要它,但标准做法是将非数字数组索引封装在单引号或双引号中,以防您使用的字符串在某些时候变为常量。

这应该是你要找的

class SomeClass {
  public $system = $GLOBALS['system'];
}

您还可以使用类常量,而不是

class SomeClass {
  const SYSTEM = $GLOBALS['system'];
}

这可以在类内用'self::SYSTEM'引用,在外部用'SomeClass::SYSTEM'引用。

于 2009-02-06T04:26:35.467 回答