Jump to content

[SOLVED] how to access associative array inside class?


Geuis

Recommended Posts

Banged my head on this for a while tonight. I think I just don't know the right terminology for Google to return the right answer.

 

Basically I have an associative array that is being declared outside of my class. I need to be able to access that array within methods in the class, but I don't want to have to pass the array to each method every time I need to call it. I'd like to make the array globally available so that I can access it within the class.

 

For example, if I use define() then I can access those variables,  but I can't if the variable is an array.

 

Here's my test class and array:

 

<$
define("definedVar","definedValue");

$myArray = array(
	'key1' => "value1",
	'key2' => "value2"
);

class myClass{
public $myArray;

function myMethod1(){
	echo count($this->$myArray);
}

function myMethod2(){
	echo definedVar;
}

}

$abcClass = new myClass();
$abcClass->myMethod1(); //returns 0, even though the count of the array is 2.

$abcClass->myMethod2();//returns "definedValue"
?>

You have several options, pass the array into the constructor setting your public variable. Pass your array directly into the internal public variable, or as you say pass it to every function, OR have it as an internal structure. But i'm guessing the last two are out because you want it to be a "global" array as you call it. Hence option 1 or 2 will suffice.

e.g.

$array = array('key1'=>"value1", 'key2'=>"value2");

class test {
  public $internalArray; // NOT GLOBALLY AVAILABLE!

  public function __construct(array $array){
    $this->internalArray = $array;
  }

  public function yourMethod(){
    echo count($this->internalArray);
  }
}

$test = new test($array);
$test->yourMethod();

 

The one problem I see is that if you are expecting to change the $array at a later time then yourMethod() will still give you the same result as before unless you reapply the new array to the class. Unfortunately from what i've test thus far you cannot declare a reference to an array (only it's internals). However I believe this can be overcome by writing a wrapper class.

Here's the answer using the SPL ArrayIterator class (a lovely wrapper for arrays). Now you can manipulate your array outside of the class and still have your "test" class call the yourMethod() function each time without having to pass the array through on each function call.

 

<?php
$array = array('key1'=>"value1", 'key2'=>"value2");

class test {
  public $internalArray; // NOT GLOBALLY AVAILABLE!

  public function __construct(myArrayIterator $array){
    $this->internalArray = $array;
  }

  public function yourMethod(){
    echo count($this->internalArray);
  }
}


class myArrayIterator extends ArrayIterator {

}

$arr = new myArrayIterator( $array );

$test = new test($arr);
$test->yourMethod();

unset($arr['key1']);

$test->yourMethod();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.