Sumit Sharma wrote:
> What I have done is declared one User class in a separate file and created
> its object there only. After this included this file in all other file which
> are using its object. So the object is creating only once and included in
> every other file only once. Now when I over write its variable it value get
> lost when I send the user in other file.
> 
> The confusion is if I have created only one object of a class and used the
> same object through out the site how its value can get lost and I think this
> is a separate issue than setting $_SESSION variables.
> 

This is roughly how it works:

<?php
//user.php

$user = new User();

class User {
        //some stuff
}

?>

<?php
//file a.php

//NEW $user object is created
include('user.php');

//do some stuff

//$user is DESTROYED, end of script
?>

<?php
//file b.php

//NEW $user object is created
include('user.php');

//do some stuff

//$user is DESTROYED, end of script
?>


If you want to keep the first $user object and persist it across pages,
then you can many different things, but you need to put it in the
session.  One example (you could also write a session class to take care
of all of your session stuff):

<?php
//user.php

class User {
        //some stuff
}

?>

<?php
//file a.php

include('user.php');

session_start();

if(isset($_SESSION['user'])) {
        $user = unserialize($_SESSION['user']);
} else {
        $user = new User();
}

//do some stuff

//save $user to session
$_SESSION['user'] = serialize($user);

//$user is DESTROYED, end of script, but $_SESSION['user'] persists
?>

<?php
//file b.php

include('user.php');

session_start();

if(isset($_SESSION['user'])) {
        $user = unserialize($_SESSION['user']);
} else {
        $user = new User();
}

//do some stuff

//save $user to session
$_SESSION['user'] = serialize($user);

//$user is DESTROYED, end of script, but $_SESSION['user'] persists
?>

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to