【问题标题】:Initialize static array for use it in other static variable of other class初始化静态数组以在其他类的其他静态变量中使用
【发布时间】:2011-08-18 01:37:58
【问题描述】:
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... 我需要初始化 Hash 数组,这有 static 字,因为我需要在其他静态函数中使用它。而这个“其他功能”需要将它用于其他静态变量...... 我需要知道如何以正确的方式实现它..

谢谢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

这意味着: " echo 赋值::GetVal("BLABLA");" 有错误...

【问题讨论】:

    标签: php arrays function variables static


    【解决方案1】:

    Assigning() 中,尝试使用self::$Hash 而不是$this-&gt;Hash 并删除global $Hash。正如您的 cmets 建议的那样,调用 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;
    }
    

    【讨论】:

    • 我更新了我的答案,以举例说明您需要做什么
    猜你喜欢
    • 2020-05-03
    • 1970-01-01
    • 2017-02-08
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 2021-05-02
    • 2023-04-02
    相关资源
    最近更新 更多