Re: generically accessing member variables problem
| From: | Marek Kilimajer | Date: | Wed, 06 Nov 2002 12:42:19 +0000 |
| Subject: | Re: generically accessing member variables problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-122975@lists.php.net to get a copy of this message | ||
If I understand you, you need to have a basic class with the
one function and subclass it. Then you can reference the array
as $this->$passed_in_array_name
John Kenyon wrote:
I'm trying to write a function I can plop in a bunch of different classes without having to modify it. Basically I have classes like: class Example { var $array1; var $array2; var $array3; etc. } and I want to have a function in that class that looks something like this: function do_something_to_array($passed_in_array_name) { //this is what I've done so far, but which isn't working $arr = '$this->' . $passed_in_array_name; // now I want $passed_in_array_name to be evaluated as an array eval ("\$arr = \"$arr\";"); // however even if 'array1' is the passed in value $arr is not the // same as $this->array1 ... } As a side note there is another aspect of this that confuses me -- if I do a print_r($arr) before the eval it returns the string '$this->array1', if I do it after it returns Array (which is what it seems it should do. However, if I then pass $arr to a function that requires an array as an argument, like array_push, for example, I get an error that says that function takes an array. Can anyone explain this to me? The only guess I have is that my eval function is turning it into a string which reads as Array instead of either a String object or the value of the string. More important though is the first problem of generically accesing a member variable based on the passed in name of the variable. In other words I want to be able to choose which array I operate on by passing in the name of that array. Any help on this problem is appreciated. I know there must be a way to do this. Please let me know if I haven't formulated the problem clearly enough. jck