Hello All,

I have built a small class in which I have an Array to which I would like to pass a variable.

This is the class:

class page
{

// These are the attributes for the page class

var $house
var $buttons = array( 'Homes' => 'homes.php?".$this->house."_homes'
);
}

And this is how I intended to pass the variable:

The other script consrtucts the page class and passes it the

$homepage = new page($house);

But what I get from this button when I place the mouse over is:

/SERVERNAME/homes.php?".$this->house."_homes

Obviously it cannot parse the variable....
I have tried it with single, double, and no quotes just to rule out a quote-related error...

Any thoughts on how I can pass the value to the Array?

    If I remember correctly, PHP doesnt allow one to initialize member variables that way.
    Rather, what you should do is:

    class Page
    {
    	var $house;
    	var $buttons;
    
    function Page($house)
    {
    	$this->house = $house; //or whatever you need to do with house
    	$this->buttons = array();
    	$this->buttons['Homes'] = 'homes.php?' . $this->house . '_homes';
    }
    }

    Actually, it might be better to create a member function to set the button array.

    I have no idea what the class does at all though so I cant say much.

      laserlight,

      yup...apparently that's how's it's gotta be...we wanted to keep the array outside the constructor (for "other reasons") but I guess were going to go for something in the direction you suggested.

      Thanks for your tip!

        8 months later

        Hey this is a pretty old topic, but I have a similar problem that isn't fixed by this here. I'm currently programming what will eventually be a fully automated site for a computer gaming league, and I was having a problem with taking an array in and assigning it as a variable in a class. I'm a past C++ programmer, and I really havn't used PHP that much, so I'm just wondering if there's a quirky language problem I don't know about. I'm so against stealing scripts I won't even take a news system without knowing exactly what happens.

        Anyways here's my code:

        class newspost
        {
        var $poster;
        var $timestamp;
        var $title;
        var $content;

        function set($poster, $timestamp, $title, $content)
        //This function sets this newspost to the input values
        {
          $this->poster=$poster;
          $this->timestamp=$timestamp;
          $this->title=$title;
          $this->content=array();
          $this->content=$content;
          //for ($i=0;$i<count($content);$i++)
          //  $this->content[$i]=$content[$i];
          echo "$content[0]";
          echo "$this->content[0]";
        }

        //... more functions
        }

        Basically what happens is the array inside the class ($this->content) isn't set to what the input array is ($content). I've tried multiple ways of assigning it, including array_slice and that commented out 'for' loop, but they all do the same thing, which is nothing. The first test echo outputs the correct value, but the second one just puts: Array[0]

          You can set variables when instantiating a class as of PHP 4.3.* afaik. There are a few exceptions to this - for one you can't concatenate, as you are trying to do

          class page { 
            // These are the attributes for the page class 
            var $house;
            var $buttons = array( 'Homes' => 'homes.php?');
          
            function page(){
              $this->buttons['homes'] .= $this->house."_homes'"; 
            }
          }
          

          hth

            Keiser,
            Just doing:
            $this->content = $content;
            Is all you need. You don't have to create an array first. That's assuming that the passed $content is an array. Of course it stores a copy of the array.

            Shrike,
            You can only initialize instance variables to constant values. Concatenation is not really the problem. It's the fact that $house is a variable.

              Agreed. I wrote that post before I had any coffee this morning 😉

                Yeah I fixed mine, it was really weird, but if you try to output it by:

                echo "$post->content[0]";
                it doesn't work, it sees $post->content and immediately outputs "Array" because that's what that variable is, and then it just outputs [0]. So anyways I just wrote a member function to return an element of the array $content, and go:

                $str=$str=$post->element($i);
                echo "Content: $str/n";

                and that fixes it. Annoying stupid little language quirk, but I'm glad I worked it out so I'll know about it later!

                  Write a Reply...