Page 1 of 1

Array variable in a PHP class problem

Posted: Mon May 08, 2006 2:23 pm
by Revme
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi Guys,

I am having trouble populating a array variable with in a PHP class with the right values

I have a simple class

Code: Select all

class tasl {

 
         var $_ip_address;
         var $_ports;


         function tasl ()
                {
                 
                 $this->$_ip_address = array();
                 $this->$_ports      = array();
                }

with functions to add IP and ports submitted from a HTML form

 function add_ip_address($ip)
                {
                        $array_list = split (',',$ip);

                        foreach ($array_list as $ip1 => $value)
                        {
                          if(valid_ip_format($array_list[$ip1]))
                           {

                           array_push($this->$_ip_address,$array_list[$ip1]);

                           }
similar code for functoin add_ports ()

When I populate the arrays and try to print them

the result is a combination of both the arrays in a single array

for e.g. if IP = 1.1.1.1, 2.2.2.2
and port = 35,34

the result is

Code: Select all

print_r($this->$_ip_address);

Array ( [0] => 1.1.1.1 [1] => 2.2.2.2 [2] => 34 [3] => 35 ) 

print_r($this->$_ports);

Array ( [0] => 1.1.1.1 [1] => 2.2.2.2 [2] => 34 [3] => 35 )
i.e all entries get populated in both the arrays

whereas I was expecting them to b in different arrays...

Can some1 point towards what i am doing wrong?

Thanks

rev


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon May 08, 2006 2:42 pm
by someberry
I don't see the point of the constructor. Just do:

Code: Select all

var $_ip_address = Array();
var $_ports = Array();
Post the whole of the class, it is 'kinda hard to spot the error those 10 lines of code.

Oh, and you dont need the $ at the begining of the variable when using it with $this ->. It should be:

Code: Select all

$this -> variable