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...
}

Hi, you can see my script or code php... I need to initialize the Hash array, this have static word because I need to use it in other static function. And this "other function" need to use it for other static variable... I need to know how to implement it the right way..

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

This code is not running, somebody help me testing the code... result:

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

that mean: " echo Assignation::GetVal("BLABLA");" have error...

1 Answer 1

1

In Assigning(), try using self::$Hash rather than $this->Hash and remove the global $Hash. Same applies for calling Assigning(): self::Assigning() as your comments suggest.

$this references the current object, so you must use self:: for all static functions and member data when inside the class.

Also, if this is your real code and not just a sample, you may want to check whether you have already done initialization, otherwise you will be doing it for every call to GetVal() and GetNam(). You could do this by adding something like if(!empty(self::$Hash)) return at the beginning of Assigning()

EDIT

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I updated my answer to give an example of what you need to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.