Why does my php class variable disappear?

  • Thread starter Thread starter joshua c
  • Start date Start date
J

joshua c

Guest
in one page I have:

session_start();
class X
{
var $test;
function X() {
$test = 1;
}
}
$X = new X;
$sX = serialize($X);
$_SESSION['sX'] = $sX;

in my index.php page I have:

session_start();
$X = unserialize($_SESSION['sX']);
if(isset($X))
{
echo "Your test variable is ."$X->test;
}

what I see on the page is:
Your test variable is

I am hoping to see "Your test variable is 1". Why am I not?
 
$X = new X;
$sX = serialize($X);
$_SESSION['sX'] = $sX;

is not in your class!
see below
class X
{
var $test;
function X() {
$test = 1;
}
} <------ Ends the class this is outside of it

below is not needed in the class file
$X = new X;
$sX = serialize($X);
$_SESSION['sX'] = $sX;

and you need to do something like this

index.php

session_start();
require_once('your class file .php');
$X = new X();
$MyX = $X->$X;
echo $MyX;
 
Back
Top