I have a situation where i need to maintain a static variable globally where it value can be change by diferent php files.
I have below class,
<?php
class FlagMe {
public static $flag;
public static function setFlag($flag) {
self::$flag = $flag;
}
public static function getFlag() {
return self::$flag;
}
}
?>
And I am setting a value from a diferent php file like below,
FlagMe::setFlag("SomeValue");
But suppose after a post request code flow in same php script (controller class) with a page refresh, and when I am try to access that static variable it gives me instead of giveing the set value “SomeValue” previously.
$temVar = FlagMe::getFlag(); // gives null
What am I missing here please?
Thanks.
Once the page is refreshed, a new execution of the script occurs. Object instances/class variables are not saved between different executions.
If you wish to save a variable, store it in a session or a cookie. Then, on every execution, check for the session and set the variable accordingly.