Class variable value lost

Class variable value lost

am 09.09.2009 17:28:53 von Sumit Sharma

--0016e6db61ca911fca047326bdf5
Content-Type: text/plain; charset=ISO-8859-1

Hi,

I have developed a listing site which is totally class based. Now when it
authenticates a user login and set appropriate class variables to true and
set user info in user class variables, value of all the set variables are
lost when I forward the user to members page. When I check the the value on
other page it is set to the default value of the class. Please help.


Regard,
Sumit

--0016e6db61ca911fca047326bdf5--

Re: Class variable value lost

am 09.09.2009 17:36:13 von Shawn McKenzie

Sumit Sharma wrote:
> Hi,
>
> I have developed a listing site which is totally class based. Now when it
> authenticates a user login and set appropriate class variables to true and
> set user info in user class variables, value of all the set variables are
> lost when I forward the user to members page. When I check the the value on
> other page it is set to the default value of the class. Please help.
>
>
> Regard,
> Sumit
>

You needs to pass the object to the next page. Look into sessions.

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

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

Re: Re: Class variable value lost

am 09.09.2009 17:44:10 von Ashley Sheridan

On Wed, 2009-09-09 at 10:36 -0500, Shawn McKenzie wrote:
> Sumit Sharma wrote:
> > Hi,
> >
> > I have developed a listing site which is totally class based. Now when it
> > authenticates a user login and set appropriate class variables to true and
> > set user info in user class variables, value of all the set variables are
> > lost when I forward the user to members page. When I check the the value on
> > other page it is set to the default value of the class. Please help.
> >
> >
> > Regard,
> > Sumit
> >
>
> You needs to pass the object to the next page. Look into sessions.
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com
>
The object only exists for that instance of the script, so when the user
navigates to the next page, the object is freed up from the memory.
There are a couple of ways you could get round this:

* don't navigate away from the page, and use AJAX calls to update
parts of the page for the user (bad imho, as it relies on
Javascript)
* use sessions like Shawn recommended

If you use sessions, you can store the objects themselves as variables
into the session. You should be careful with this, as you may not want
too many sessions open with large objects in them as, depending on your
server setup, sessions could last quite a while, and there may also be a
limit on the amount of memory reserved for sessions.

Thanks,
Ash
http://www.ashleysheridan.co.uk




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

Fwd: Re: Class variable value lost

am 09.09.2009 17:58:26 von Sumit Sharma

--001485f423dc4a7a8c0473272701
Content-Type: text/plain; charset=ISO-8859-1

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.





---------- Forwarded message ----------
From: Ashley Sheridan
Date: Wed, Sep 9, 2009 at 9:14 PM
Subject: Re: [PHP] Re: Class variable value lost
To: Shawn McKenzie
Cc: Sumit Sharma , PHP General Mailing List <
php-general@lists.php.net>


On Wed, 2009-09-09 at 10:36 -0500, Shawn McKenzie wrote:
> Sumit Sharma wrote:
> > Hi,
> >
> > I have developed a listing site which is totally class based. Now when
it
> > authenticates a user login and set appropriate class variables to true
and
> > set user info in user class variables, value of all the set variables
are
> > lost when I forward the user to members page. When I check the the value
on
> > other page it is set to the default value of the class. Please help.
> >
> >
> > Regard,
> > Sumit
> >
>
> You needs to pass the object to the next page. Look into sessions.
>
> --
> Thanks!
> -Shawn
> http://www.spidean.com
>
The object only exists for that instance of the script, so when the user
navigates to the next page, the object is freed up from the memory.
There are a couple of ways you could get round this:

* don't navigate away from the page, and use AJAX calls to update
parts of the page for the user (bad imho, as it relies on
Javascript)
* use sessions like Shawn recommended

If you use sessions, you can store the objects themselves as variables
into the session. You should be careful with this, as you may not want
too many sessions open with large objects in them as, depending on your
server setup, sessions could last quite a while, and there may also be a
limit on the amount of memory reserved for sessions.

Thanks,
Ash
http://www.ashleysheridan.co.uk

--001485f423dc4a7a8c0473272701--

Re: Re: Class variable value lost

am 09.09.2009 18:03:30 von Andrew Ballard

On Wed, Sep 9, 2009 at 11:58 AM, 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 happens because your object is destroyed as soon as PHP finishes
serving the request. That's the way the web was designed. If you want
your object to persist from one request to another, YOU have to
persist it. There are lots of ways to do that. Storing your object in
a $_SESSION variable is one of those ways.

Andrew

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

Re: Re: Class variable value lost

am 09.09.2009 20:19:48 von Martin Scotta

--000e0cd48814dbc7b3047329204f
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Sep 9, 2009 at 12:58 PM, 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.
>
>
>
>
>
> ---------- Forwarded message ----------
> From: Ashley Sheridan
> Date: Wed, Sep 9, 2009 at 9:14 PM
> Subject: Re: [PHP] Re: Class variable value lost
> To: Shawn McKenzie
> Cc: Sumit Sharma , PHP General Mailing List <
> php-general@lists.php.net>
>
>
> On Wed, 2009-09-09 at 10:36 -0500, Shawn McKenzie wrote:
> > Sumit Sharma wrote:
> > > Hi,
> > >
> > > I have developed a listing site which is totally class based. Now when
> it
> > > authenticates a user login and set appropriate class variables to true
> and
> > > set user info in user class variables, value of all the set variables
> are
> > > lost when I forward the user to members page. When I check the the
> value
> on
> > > other page it is set to the default value of the class. Please help.
> > >
> > >
> > > Regard,
> > > Sumit
> > >
> >
> > You needs to pass the object to the next page. Look into sessions.
> >
> > --
> > Thanks!
> > -Shawn
> > http://www.spidean.com
> >
> The object only exists for that instance of the script, so when the user
> navigates to the next page, the object is freed up from the memory.
> There are a couple of ways you could get round this:
>
> * don't navigate away from the page, and use AJAX calls to update
> parts of the page for the user (bad imho, as it relies on
> Javascript)
> * use sessions like Shawn recommended
>
> If you use sessions, you can store the objects themselves as variables
> into the session. You should be careful with this, as you may not want
> too many sessions open with large objects in them as, depending on your
> server setup, sessions could last quite a while, and there may also be a
> limit on the amount of memory reserved for sessions.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>


Unless you store the object state your application will not be able to
remember it.

There is a design pattern specially designed for this, the Memento Pattern (
http://en.wikipedia.org/wiki/Memento_pattern)

There are many ways to persist an object: sessions, xml, text files,
databases.
Pick the one that fits better and you'll be fine.

--
Martin Scotta

--000e0cd48814dbc7b3047329204f--

Re: Fwd: Re: Class variable value lost

am 09.09.2009 20:24:46 von Shawn McKenzie

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:

//user.php

$user = new User();

class User {
//some stuff
}

?>

//file a.php

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

//do some stuff

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

//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):

//user.php

class User {
//some stuff
}

?>

//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
?>

//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

Re: Re: Class variable value lost

am 09.09.2009 20:40:31 von Ben Dunlap

> The object only exists for that instance of the script, so when the user
> navigates to the next page, the object is freed up from the memory.
> There are a couple of ways you could get round this:
>
> =A0 =A0 =A0* don't navigate away from the page, and use AJAX calls to upd=
ate
> =A0 =A0 =A0 =A0parts of the page for the user (bad imho, as it relies on
> =A0 =A0 =A0 =A0Javascript)

I think any AJAX-based approached would run into the same difficulty,
because each AJAX call is a separate HTTP request from the one that
originally loaded the page (and from every other AJAX call).

Ben

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