public function actionTest()
{
$x=5;
$y=10;
function myTest()
{
global $x,$y;
$y=$x+$y;
}
myTest();
echo $y; // The outputs must be 15 but is 10!
}
The outputs of Test action must be [color="#2E8B57"]15 [/color]but is [color="#FF0000"]10[/color]! I tried it on individual script out of the framework an the output was correct([color="#008000"]15[/color]).
The global scope is not the public function actionTest() { … } method body - the global scope is the index.php scope.
For example:
$a = 1;
function foo() {
$a = 2;
function bar() {
global $a;
var_dump($a); // print int(1) not int(2)
}
bar();
}
foo();
Some solution:
use global in actionTest():
public function actionTest()
{
global $x,$y;
$x=5;
$y=10;
function myTest()
{
global $x,$y;
$y=$x+$y;
}
myTest();
echo $y; // The outputs must be 15 but is 10!
}
or use $GLOBALS array:
public function actionTest()
{
$GLOBALS["x"]=5;
$GLOBALS["y"]=10;
function myTest()
{
$GLOBALS["y"]=$GLOBALS["x"]+$GLOBALS["y"];
}
myTest();
echo $GLOBALS["y"]; // The outputs must be 15 but is 10!
}
or put the myTest to the class and use $this:
class XY {
private $x = 0;
private $y = 0;
// ...
private function myTest()
{
$this->y=$this->x+$this->y;
}
public function actionTest()
{
$this->x=5;
$this->y=10;
$this->myTest();
echo $this->y; // The outputs must be 15 but is 10!
}
}