My variables are not working, and I'm not sure why. I did not define $var as static, so when I reference it as global, or as $this->var, it should be the same variable, right? Except it isn't.
Some people say 'global' shouldn't be used, instead pass parameters to the function. But what if I need to work with 20 variables in an instance's function? Do I really pass 20 parameters to it? Doesn't it become unreadable and unclear?
I'm running PHP 7.2.8. on XAMPP, but that isn't really relevant.
<?php
class Test{
public $var;
public function __construct($param)//1
{
global $var; //5
$this->var = $param; //1
$var = $param * 5; //5
}
public function wtf(){
global $var; //5
$foo = $this->var; //1
echo "var: $var <br>";
echo "this var: $foo <br>";
}
}
$foo = new Test(1);
$foo->wtf();
$value = $foo->var;
echo "Value: $value";
?>
Output:
var: 5
this var: 1
Value: 1
I'm expecting the $var to be the same thing in both cases. Why does it become two?