Hi,

Thursday, November 7, 2002, 3:11:12 AM, you wrote:
MM> You can do:

${"this->>$passed_in_array_name"}

MM> not sure right now of the correct syntaxing, I never do that - normally
MM> I'd pass the element key.


MM> --
MM> Maxim Maletsky
MM> [EMAIL PROTECTED]



MM> 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
>> 

Amazing what you learn on this list :)
This works:

class Example
{
   var $array1 = array();
   var $array2 = array();
   var $array3 = array();

function add2array($array_name,$val){
        $this->{"$array_name"}[] = $val;
}

}
$t = new Example();
$t->add2array('array1',25);
$t->add2array('array2',26);
$t->add2array('array3',"Hello");
echo '<pre>';
print_r($t);
echo '</pre>';

-- 
regards,
Tom


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to