[PHP] generically accessing member variables problem
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] generically accessing member variables problem
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] generically accessing member variables problem
Thanks, that may be what I was looking for. jck Maxim Maletsky wrote: You can do: ${"this->$passed_in_array_name"} not sure right now of the correct syntaxing, I never do that - normally I'd pass the element key. -- Maxim Maletsky [EMAIL PROTECTED] John Kenyon <[EMAIL PROTECTED]> wrote... : 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] generically accessing member variables problem
See below: MM> class Example { MM> var $array = array(); MM> function add2array($element_name, $val){ MM> $this->$array[$element_name] = $val; MM> } MM> } MM> $t = new Example(); $t->>add2array('array1',25); $t->>add2array('array2',26); $t->>add2array('array3',"Hello"); MM> echo ''; MM> print_r($t); MM> echo ''; MM> Cleaner and more scalable, no? Yes and to fit the original 3 seperate arrays it would be function add2array($element_name, $val){ $this->$array[$element_name][] = $val; } No, because you pass it the name and data. This way every name will become element's name containing the relevant data. Your way just makes it an associative array without a way of directly accessing it. -- Maxim Maletsky [EMAIL PROTECTED] But my problem is that I have several arrays already and I want to be able to act on a specific array depending on the name I pass in. I don't see how your solution solves that issue. jck -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] generically accessing member variables problem
I am not really sure I understand what you are saying here, and I would like to. Let me first say that I think the syntax you came up with earlier will solve my immediate problem, but if I could design this in a better way I'd like to know. Let me give you a few more details: I have a large class that contains multiple instances of various different classes, in turn, each of these instances could contain multiple instances of other classes. A big tree. In order to keep track of the various instances of the classes I use arrays, so each class that contains instances of other classes keeps like classes within an array. Sometimes a class has multiple arrays, each holding instances of a different type of class. Each of these classes is being used to generate html forms and I want to have a way to have the user check a checkbox and remove a particular instance of a class. I want to have one function that I can plop into each class (in some cases it may be possible to have children inherit the function from a parent class) and which will delete the correct instance of the correct class by passing in the name of the array the instance is held in and its index in that array. I hope this provides you with a little more context and if your advice still pertains, please explain it if you would. jck Maxim Maletsky wrote: You pass it the name of the element, and whatever the data inside. You do not need to add other sub-elements to it automatically, as you would need to be "searching" through the elements later for the right data. Whatever your need is - it's a good idea using arrays, and add other arrays into it. But, it is a bad idea cloning variables and auto-assign array's elements when you know that you will need that specific piece you store alone. -- Maxim Maletsky [EMAIL PROTECTED] John Kenyon <[EMAIL PROTECTED]> wrote... : See below: MM> class Example { MM> var $array = array(); MM> function add2array($element_name, $val){ MM> $this->$array[$element_name] = $val; MM> } MM> } MM> $t = new Example(); $t->>add2array('array1',25); $t->>add2array('array2',26); $t->>add2array('array3',"Hello"); MM> echo ''; MM> print_r($t); MM> echo ''; MM> Cleaner and more scalable, no? Yes and to fit the original 3 seperate arrays it would be function add2array($element_name, $val){ $this->$array[$element_name][] = $val; } No, because you pass it the name and data. This way every name will become element's name containing the relevant data. Your way just makes it an associative array without a way of directly accessing it. -- Maxim Maletsky [EMAIL PROTECTED] But my problem is that I have several arrays already and I want to be able to act on a specific array depending on the name I pass in. I don't see how your solution solves that issue. jck -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] generically accessing member variables problem
Ok. I think I got it. Now I have to look at my code again to see if I can implement it. Thanks for your time. jck Maxim Maletsky wrote: Well, if, say, one class has this function: function inherit($class_name, $pointer) { $this->objects[$class_name] = $pointer; } then you end up knowing that, whenever you need to access an instance for a class, you can use $this->objects['that_class']->that_array. What I picked on, was that you were assigning keys automatically with ($array[] = ...) and that makes it impossible pointing to the right element directly without having to loop the whole array, which is an overkill. The rest of your logic is pretty complex but doable. As long as you plan off well the complete OOD for that. So, my conclusion is: It is OK storing all objects in one single array, but name then the way you can find them later. If you have multiple instances of the same class, use numeric keys but assign them under a certain logic - not automatically, so you can find/use/destroy them at any point. can't give you more info here as that is all I know of it. Check out the CVs tree for ZoomStats - www.zoomstats.org, it is a 100% OOP PHP app and can give you some hints. -- Maxim Maletsky [EMAIL PROTECTED] John Kenyon <[EMAIL PROTECTED]> wrote... : I am not really sure I understand what you are saying here, and I would like to. Let me first say that I think the syntax you came up with earlier will solve my immediate problem, but if I could design this in a better way I'd like to know. Let me give you a few more details: I have a large class that contains multiple instances of various different classes, in turn, each of these instances could contain multiple instances of other classes. A big tree. In order to keep track of the various instances of the classes I use arrays, so each class that contains instances of other classes keeps like classes within an array. Sometimes a class has multiple arrays, each holding instances of a different type of class. Each of these classes is being used to generate html forms and I want to have a way to have the user check a checkbox and remove a particular instance of a class. I want to have one function that I can plop into each class (in some cases it may be possible to have children inherit the function from a parent class) and which will delete the correct instance of the correct class by passing in the name of the array the instance is held in and its index in that array. I hope this provides you with a little more context and if your advice still pertains, please explain it if you would. jck Maxim Maletsky wrote: You pass it the name of the element, and whatever the data inside. You do not need to add other sub-elements to it automatically, as you would need to be "searching" through the elements later for the right data. Whatever your need is - it's a good idea using arrays, and add other arrays into it. But, it is a bad idea cloning variables and auto-assign array's elements when you know that you will need that specific piece you store alone. -- Maxim Maletsky [EMAIL PROTECTED] John Kenyon <[EMAIL PROTECTED]> wrote... : See below: MM> class Example { MM> var $array = array(); MM> function add2array($element_name, $val){ MM> $this->$array[$element_name] = $val; MM> } MM> } MM> $t = new Example(); $t->>add2array('array1',25); $t->>add2array('array2',26); $t->>add2array('array3',"Hello"); MM> echo ''; MM> print_r($t); MM> echo ''; MM> Cleaner and more scalable, no? Yes and to fit the original 3 seperate arrays it would be function add2array($element_name, $val){ $this->$array[$element_name][] = $val; } No, because you pass it the name and data. This way every name will become element's name containing the relevant data. Your way just makes it an associative array without a way of directly accessing it. -- Maxim Maletsky [EMAIL PROTECTED] But my problem is that I have several arrays already and I want to be able to act on a specific array depending on the name I pass in. I don't see how your solution solves that issue. jck -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] I'm in need of a PHP web host recommendation
Jason Reid wrote: I suggest paying a visit to www.webhostingtalk.com and search the forums... theres tons of information on the large, and small hosts that might help. I use www.phpwebhosting.com and have been happy with them so far. $10 a month, but they are generous (and flexible, at least so they say) about space and bandwidth. jck -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php