tags:

views:

39

answers:

3

is it possible to copy a variable like this this?

class Colours {
   var $var = "one";
   var $var2 = array('something', $var);
}
+2  A: 

You'd need to use $this->var to access the variable

class Colours {
   var $var = "one";
   var $var2 = array('something', $this->var);
}
adam
This won't work because at the time of class declaration, there is no `$this` present yet.
Pekka
+6  A: 

The preferable way is to do this in the constructor of the Colours class. I'm not sure in PHP, but in other languages the order of initialisation of the variables should not be relied upon.

class Colours 
{ 
    private $var;
    private $var2;

    public function __construct()
    {
        $this->var = "one";
        $this->var2 = array('something', $this->var);
    }
}
Andy Shellam
A: 
 <?php
     $var = "one";
     $var2 = array('something', $var);
    print_r($var2)
    ?>


 I got the following output 



     Array 
    (
            [0] => something
            [1] => one
    )
muruga
He means in a class. In which it would be $this->var.
JonnyLitt