A simplifed code snipped of a problem thats driving me crazy is below. I have created a class, initialized it in the constructor and save an instance of the class in a $_SESSION variable. The ID variable is initialized in the constructor to 1. I can retrieve the class data fine but I can't change it. In the snippet below I call a class function to change the ID variable to 2 and it shows it changing fine, but when I list the contents of the $SESSION variable it shows it not being changed. Further, if I list the object contents using the class intstance that was returned, it shows the ID with the changed value of 2.
I can't figure this out. Am I making some kind of newbie error. It seems like I have two instances of the class. I have shown the code, output and the session portion of phpinfo(); My php version is 4.3.8 on XP Pro.
Code
<?php
session_start();
class Store
{
var $ZipCode;
var $ID;
function Store()
{
$this->ZipCode = "99999";
$this->ID = 1;
}
function SetID( $ID )
{
$this->ID = $ID;
echo "<br>Set ID in Class = ";
echo $this->ID;
}
function ShowID( )
{
echo "<br>Show ID = in Class = ";
echo $this->ID;
}
}
function print_vars($obj)
{
$arr = get_object_vars($obj);
while (list($prop, $val) = each($arr))
echo "\t$prop = $val\n";
echo "<br>";
}
function ShowSessionVariables ( )
{
echo "<br>******_SESSION*****************<br>";
while(list($key, $value) = each($_SESSION))
{
echo "$key = $value<br>";
print_vars ( $value );
}
reset ( $_SESSION );
}
$st = new Store();
echo "After Constructor";
$_SESSION [ 'st' ] = $st;
ShowSessionVariables ( );
echo "Print Vars";
print_vars ( $st );
$st->SetID ( 2 );
echo "<br>After Changing ID ";
ShowSessionVariables ( );
echo "Print Vars";
print_vars ( $st );
echo $st->ID;
echo $st->ShowID();
ShowSessionVariables ( );
?>
OUTPUT with comments
After Constructor
***_SESSION**************
st = Object
ZipCode = 99999 ID = 1
Print Vars ZipCode = 99999 ID = 1
// All fine to this point
// Set ID to 2
Set ID in Class = 2
After Changing ID
***SESSION***********
st = Object
ZipCode = 99999 ID = 1 // Session vars still show it as 1
Print Vars ZipCode = 99999 ID = 2 Print vars show it changed
2
Show ID = in Class = 2 Call to class function show it changed
SESSION**************
st = Object
ZipCode = 99999 ID = 1 // $SESSION variable not changed
php.ini variables
Session Support enabled
Registered save handlers files user
Directive Local Value Master Value
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_lifetime 0 0
session.cookie_path / /
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid Off Off😕 😕