Why does my php class variable disappear?

  • Thread starter Thread starter DzSoundNirvana
  • Start date Start date
D

DzSoundNirvana

Guest
$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;
 
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?
I figured it out. Finally.
It's because my function constructor needed:

$this->test = 1;

instead of

$test = 1;
//This creates a local scope variable called $test which dies at the end of the function.
 
Back
Top