2011-08-18 61 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