so I have this problem I have 3 files like
2.php
<?php
$variable = 4;
?>
1.php
<?php
class foo {
function bar() {include_once('2.php');}
}
?>
index.php
<?php
include_once('1.php');
$foo = new foo;
foo->bar();
echo $variable;
?>
Why it tells me variable has no value ?
If I do like this
<?php
include_once('1.php');
$foo = new foo;
foo->bar();
include_once('2.php');
echo $variable;
?>
It won’t work either.
Only in this way
<?php
include_once('1.php');
$foo = new foo;
//foo->bar();
include_once('2.php');
echo $variable;
?>
It will work, any explanation?
Thanks
You are confusing
includeandinclude_once.When you use the latter the file loading and thus variable declaration will only occur once. And if you do so within the method scope, it won’t get declared a second time in the global scope. Or vice versa.