Re: generically accessing member variables problem

From: Date: Wed, 06 Nov 2002 18:29:03 +0000
Subject: Re: generically accessing member variables problem
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-123035@lists.php.net to get a copy of this message
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 maxim@php.net John Kenyon <jck@croanoke.com> 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 '<pre>'; > >>MM> print_r($t); > >>MM> echo '</pre>'; > >> > >> > >>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 > >maxim@php.net > > > > > 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 >

« previous php.general (#123035) next »