Read this.
And you can do any one of the following:
Pass the value into the constructor as a parameter (recommended option)
<?php
$value[0]=11;
$value[1]=22;
$value[2]=33;
class test {
var $code1,$code2,$code3;
function __construct($value) {
$this->code1 = $value[0];
$this->code2 = $value[1];
$this->code3 = $value[2];
echo $this->code1;
}
}
$obj = new test($value);
?>
Use the $GLOBALS array (docs)
<?php
$value[0]=11;
$value[1]=22;
$value[2]=33;
class test {
var $code1,$code2,$code3;
function __construct() {
$this->code1 = $GLOBALS['value'][0];
$this->code2 = $GLOBALS['value'][1];
$this->code3 = $GLOBALS['value'][2];
echo $this->code1;
}
}
$obj = new test;
?>
Use the global keyword (docs)
<?php
$value[0]=11;
$value[1]=22;
$value[2]=33;
class test {
var $code1,$code2,$code3;
function __construct() {
global $value;
$this->code1 = $value[0];
$this->code2 = $value[1];
$this->code3 = $value[2];
echo $this->code1;
}
}
$obj = new test;
?>
NOTES
I have corrected a couple of errors above.
You should use $this->code1 instead of $this->$code1. The second version is valid syntactically, but means something else. Consider the following example:
class myClass {
public $myVar = "My Var";
public $anotherVar = "Another Var";
function __construct () {
// creates a local variable to the constructor, called $myVar
// does NOT overwrite the value defined above for the object property
$myVar = "anotherVar";
echo $myVar; // echoes 'anotherVar'
echo $this->myVar; // echoes 'My Var'
echo $this->$myVar; // echoes 'Another Var'
}
}
Also, the above example illustrates the reason why you should use echo $this->code1; and not simply echo $code1;