2011-08-18 46 views
0
class Assignation { 
    private $VVal_1 = 1; 
    private $VNam_1 = "One"; 
    //....Multiple Items 
    private $VVal_2000 = 2000; //For Example 
    private $VNam_2000 = "Two Thousands"; //For Example 
    private static $Hash = array(); //How to initialize??? 

    private static function Assigning(){ 
    //This function for to assign the array elements (initialize) 
    global $Hash; 
    $this->Hash = array(); //to empty Hash variable and not add more data than it should. 
    $this->Hash[$this->VVal_1] = $this->VNam_1; 
    //....Multiple Items 
    $this->Hash[$this->VVal_2000] = $this->VNam_2000; 
    } 

    public static function GetVal($Nam) { 
    $this->Assigning(); //or use self::Assigning(); //I want to avoid this call 
    if (array_search($Nam, $this->Hash)) 
     return array_search($Nam, $this->Hash); 
    return -1;//error 
    } 

    public static function GetNam($Val) { 
    $this->Assigning(); //or use self::Assigning(); //I want to avoid this call 
    if (array_key_exists($Val, $this->Hash)) 
     return $this->Hash[$Val]; 
    return "Error"; 
    } 
} 

Class Testing { 
    static $OtherVal = Assignation::GetVal("BLABLA"); //for example 
    static $OtherNam = Assignation::GetNam(20); //for example 
    //Other functions... 
} 

喜的其他靜態變量靜態數組,你可以看到我的腳本或代碼的PHP ... 我需要初始化哈希陣列,這有靜態詞,因爲我需要在其他靜態函數中使用它。而這個「其他功能」需要用於其他靜態變量... 我需要知道如何以正確的方式來實現它..初始化使用它在其他類

謝謝chep.-。


<?php 
echo "pre-Class Assignation<br/>"; 
class Assignation { 
    private $VVal_1 = 1; 
    private $VNam_1 = "One"; 
    private $VVal_2K = 2000; 
    private $VNam_2K = "Two Thousands"; 
    private static $Hash = array(); 

    private static function Assigning(){ 
    if(!empty(self::$Hash)) return; 
    self::$Hash[$this->VVal_1] = $this->VNam_1; 
    self::$Hash[$this->VVal_2K] = $this->VNam_2K; 
    } 

    public static function GetVal($Nam) { 
    self::Assigning(); 
    if (array_search($Nam, self::$Hash)) return array_search($Nam, self::$Hash); 
    return -1;//error 
    } 

    public static function GetNam($Val) { 
    self::Assigning(); 
    if (array_key_exists($Val, self::$Hash)) return self::$Hash[$Val]; 
    return "Error"; 
    } 
} 
echo "post-Class Testing<br/>"; 
echo Assignation::GetVal("BLABLA"); 
echo "post-Class Mid<br/>"; 
echo Assignation::GetNam(20); 
echo "post-Class Sample<br/>"; 
//Testing::MyPrint(); 
?> 

這個代碼不運行時,有人幫我測試代碼... 結果:

pre-Class Assignation 
post-Class Assignation 
post-Class Testing 

意思: 「回聲分配:: GETVAL(」 BLABLA「) ;」 有錯誤...

回答

1

Assigning(),請嘗試使用self::$Hash而不是$this->Hash並取出global $Hash。根據您的意見建議,撥打Assigning()self::Assigning()同樣適用。

$this引用當前對象,因此您必須在類中使用self::來表示所有靜態函數和成員數據。此外,如果這是您的真實代碼而不僅僅是一個樣本,您可能想要檢查您是否已經完成了初始化,否則您將在每次調用GetVal()GetNam()時都這樣做。您可以通過在Assigning()

編輯的開頭添加類似if(!empty(self::$Hash)) return做到這一點

private static function Assigning() { 
    if(!empty(self::$Hash)) return; // already populated 
    self::$Hash = array(); 
    self::$Hash[$this->VVal_1] = $this->VNam_1; 
    //....Multiple Items 
    self::$Hash[$this->VVal_2K] = $this->VNam_2K; 
} 
+0

我的文件被稱爲「StaticCallings.php」和代碼是: –

+0

我更新我的回答給的例子你需要做什麼 – steveo225