For some reason my static variable resets and I am not sure why it resets.
public static $_pwdAdjId = 0; //static variable
function ...() {
// Piece of code that manipulates the static variable
if(isset($_POST['adj'])) {
self::$_pwdAdjId = $_POST['adj'];
} else if(!isset($_POST['adj']) && $_GET['ajax'] !== 'assignTable') {
self::$_pwdAdjId = Yii::app()->user->getId();
}
$adj = self::$_pwdAdjId;
...
}
When the function is first called, this will be called:
self::$_pwdAdjId = Yii::app()->user->getId();
and saves the value into $adj and it works fine.
When the user changes the page the value that is stored into $adj is 0 – the initial value of the static variable. I am not sure why it is reseting to its initial value instead of the new value stored.
Before First Call:
self::$_pwdAdjId == 0
First Call:
self::$_pwdAdjId == 7
$adj == 7
Page (ajax call): Second Call:
self::$_pwdAdjId == 0 // supposed to be 7
Am I missing something? Does the fact that it’s called via ajax the second time affect its static-ness?
Thanks.
When the user changes the page, it’s a totally different HTTP request and your program runs again from scratch.
staticdoes not do what you think it does. You can achieve the result you want using session variables.Since it looks like you are using Yii framework, you might want to use its own wrapper over PHP sessions instead of rolling your own (by the way, it also has wrappers for getting HTTP request parameters).