I'm new to php, so I'm not sure if I can do this. I've a class which has a static variable which I'm periodically changing with the help of a static function like below. I'm reading the static variable through a getter method in another static function.
infi_file.php
class infi_file {
private static $value = 1;
static function run_infinite ($interval) {
while (true) {
print date("H:i:s") . " - " . infi_file::$value . "\n";
sleep ($interval);
infi_file::$value = infi_file::$value ^ 1;
}
}
static function getMode () {
return infi_file::$value;
}
}
run_infi_file.php
require_once "infi_file.php";
infi_file::run_infinite (5);
get_value.php
require_once "infi_file.php";
print infi_file::getMode () . "\n";
So I'm running both run_infi_file.php and get_value.php files simultaeously on 2 different terminal tabs.
While the output for run_infi_file is something like this :
15:11:51 - 1
15:11:56 - 0
15:12:01 - 1
15:12:06 - 0
...
the output of get_value always seems to be 1, even when the value is changed by run_infinite function. I thought that since $value is a static variable the entire class has just one copy which's used by all functions in that class. Why is this not happening?