Geuis Posted April 25, 2008 Share Posted April 25, 2008 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" ?> Link to comment https://forums.phpfreaks.com/topic/102851-solved-how-to-access-associative-array-inside-class/ Share on other sites More sharing options...
aschk Posted April 25, 2008 Share Posted April 25, 2008 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. Link to comment https://forums.phpfreaks.com/topic/102851-solved-how-to-access-associative-array-inside-class/#findComment-526957 Share on other sites More sharing options...
aschk Posted April 25, 2008 Share Posted April 25, 2008 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(); ?> Link to comment https://forums.phpfreaks.com/topic/102851-solved-how-to-access-associative-array-inside-class/#findComment-526959 Share on other sites More sharing options...
Geuis Posted April 27, 2008 Author Share Posted April 27, 2008 Thank you aschk! Not only did you answer my question, I understand what constructors are for now. I did not know they were run automatically. Thank you! Link to comment https://forums.phpfreaks.com/topic/102851-solved-how-to-access-associative-array-inside-class/#findComment-528172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.