updating class array member variables in function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vijayphan
Forum Newbie
Posts: 1
Joined: Mon Aug 18, 2008 9:53 pm

updating class array member variables in function

Post by vijayphan »

Hi

I am new to php.

I have a query. I have a class which has a array member variable list1 and list2. Am trying to update it in a function and not able to.

Here is the class. Please help me.
Code: ( text )
<?php
class simple {
var $list1;
var $list2;

function updateValue() {
$fd = fopen("/mapping", "r");
$i=0;

while(!feof($fd)) {
$info = fscanf($fd, "%s %s\n");
if ($info) {
list($name, $version) =$info;
$this->list1[$i] = $name;
$this->list2[$i] = $version;
$info = null;
$i++;
}
}
fclose($fd);
echo "$this->list1[0]";
echo "$this->list1[1]";
echo "$this->list1[2]";
}
}
?>
I get the response as Array(0), Array(1) and Array(2) instead of strings as dev0, dev1, dev2.

The file has the following:

dev0 1.2.3
dev1 2.4.5
dev2 2.6.7

Thanks & Regards
VP
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: updating class array member variables in function

Post by nowaydown1 »

Welcome to the forum! Drop the string interpolation and you should be good:

Code: Select all

 
echo $this->list1[0];
echo $this->list1[1];
echo $this->list1[2];
 
Instead of:

Code: Select all

 
echo "$this->list1[0]";
echo "$this->list1[1]";
echo "$this->list1[2]";
 
Post Reply