Re[2]: [PHP] generically accessing member variables problem
| From: | Tom Rogers | Date: | Wed, 06 Nov 2002 17:56:49 +0000 |
| Subject: | Re[2]: [PHP] generically accessing member variables problem | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-123032@lists.php.net to get a copy of this message | ||
Hi,
Thursday, November 7, 2002, 3:45:34 AM, you wrote:
MM> Yet, this is not a such elegant way doing it. It can be helpful in a lot
MM> of cases, but most often, element key is enough. Try rethinking your
MM> logic:
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;
}
--
regards,
Tom