I have like:
class Class2 extends Class1 {
.....
function __construct() {
parent::__construct();
$var_in_included_file;
}
}
class Class1 {
function __construct() {
include_once('my_file.php')
}
.....
}
my_file.php:
$var_in_included_file=10;
The problem is that I cannot receive value of $var_in_included_file. Is there a way to receive this value without add many codice like:
$this->var=$var_in_included_file ....?
Because I have many thousands of variables.
Thanks.
More abstract the problem is:
in some file I received (from $_POST) near 500 variable.
These variables have be elaborated in complicated way. For simplify this elaborating I need create tree of class inheritans – but in this case these variables will not seen in child classes without assigning them to class variables – but this produses enormous volume of code.
As explained in
include()and variable scopes, when you include a file in your__construct()method, the scope of the variables in the file you’re including is limited to the__construct()method, not the class.Your options would be to either change the content of the included file to include a
$this->in front of the variable name (i.e.$this->var_in_included_file = 10;) or add a$this->var_in_included_file = $var_in_included_file;in your__construct()method.