Re: generically accessing member variables problem
| From: | John Kenyon | Date: | Wed, 06 Nov 2002 16:45:50 +0000 |
| Subject: | Re: generically accessing member variables problem | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-123022@lists.php.net to get a copy of this message | ||
Thank you for replying, but I don't think I've made my problem clear enough. Let me give it another shot.
What I want is a function that takes the name of an array as a parameter so that it can be popped into any class that has arrays in it and work without modification. The problem I am having is in converting the passed in array name to the form which the class would recognize as its own member variable, i.e. $this->passed_in_array_name. In other words, it comes in as a string, just the name of the array, I append $this-> to it, but it still isn't getting interpreted the same as if I had written the call explicitely to $this->memberarray. Does this make any more sense?
jck
Marek Kilimajer wrote:
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