2009-02-06 36 views
12

我在全局範圍內有一個名爲${SYSTEM}的變量,其中SYSTEM是一個定義的常量。我有很多需要訪問這個變量的函數的類,我發現它每一次都會發出聲明global ${SYSTEM};的惱人的類。爲一個類中的每個函數創建一個全局變量

我試着聲明一個類變量:public ${SYSTEM} = $GLOBALS[SYSTEM];但這會導致語法錯誤,這很奇怪,因爲我有另一個類以這種方式聲明類變量,似乎工作正常。我能想到的唯一的事情就是常數沒有被識別。

我已經設法用一個構造函數來解決這個問題,但我正在尋求一個更簡單的解決方案,然後再訴諸於此。


編輯 全球$ {}系統變量是一個有很多在它的其他子數組的數組。不幸的是,似乎沒有辦法避開使用構造函數...

回答

1

成員變量的直接指定不能包含對其他變量的任何引用(class {public $membervar = $outsidevar;}也是無效的)。改爲使用構造函數。

但是,由於您正在處理常量,爲什麼不使用php的constantclass constant設施?

-2

我會說,站出來給我的第一個兩件事情是:

  1. 你並不需要在變量名稱周圍的支架,你可以簡單地做公益$系統或公共$ SYSTEM。
  2. 儘管PHP可能並不總是需要它,但是標準做法是將非數字數組索引封裝在單引號或雙引號中,以防您正在使用的字符串在某個點變成常量。

這應該是你在找什麼

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

您還可以使用類常量這將改爲

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

這可以用「自我類中被引用: :SYSTEM',外部使用'SomeClass :: SYSTEM'。

+0

這是否意味着常數>變量的名字呢? – atomicharri 2009-02-06 04:45:49

+0

在這個問題中,SYSTEM是一個常量,而不是變量名。 – PolyThinker 2009-02-06 04:47:40

+0

我不知道你在說什麼,但$ {SYSTEM}絕對不是和$ SYSTEM一樣...... – atomicharri 2009-02-06 04:56:03

-1

你也可以嘗試singleton模式,雖然在某種程度上它在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(); 

?> 

這個例子是從維基百科稍微修改,但你可以得到的想法。嘗試使用Google Singleton模式以獲取更多信息

+0

我不明白這與單身人士有什麼關係。 – PolyThinker 2009-02-06 04:53:38

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(); 
?> 
1

你正在試圖做的這裏有一些非常不尋常的東西,所以你可以期望它很尷尬。使用全局變量永遠不會令人愉快,特別是不使用動態名稱選擇使用SYSTEM常量。個人而言,我會建議你使用$GLOBALS[SYSTEM]無處不在,而不是,或...

$sys = $GLOBALS[SYSTEM]; 

...如果你要很多使用它。

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(); 

?> 
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類的實例的引用。

固定格式

-2
class Dateutility { 

    public $a,$b; 
    public function getCurrentTime() { 
     date_default_timezone_set("Asia/Karachi"); 
     echo "The current time is: "; 
     echo date("g:i a"); 
    } 

    public function seta($c) { 
     $a=$c; 
     echo "<br/>value of a is:".$a; 
    } 

    public function setb($d) { 
     $b=$d; 
     echo "value of b is:".$b; 
    } 

} 

$vari = new Dateutility;   
$vari->getCurrentTime(); 

$vari->seta(10); 
$vari->setb(20); 
相關問題