[PHP] Re: Class variable value lost
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: [PHP] Re: Class variable value lost
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: [PHP] Re: Class variable value lost
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 a...@ashleysheridan.co.uk Date: Wed, Sep 9, 2009 at 9:14 PM Subject: Re: [PHP] Re: Class variable value lost To: Shawn McKenzie nos...@mckenzies.net Cc: Sumit Sharma sumitp...@gmail.com, 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
Re: [PHP] Re: Class variable value lost
On Wed, Sep 9, 2009 at 11:58 AM, Sumit Sharma sumitp...@gmail.com 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: [PHP] Re: Class variable value lost
On Wed, Sep 9, 2009 at 12:58 PM, Sumit Sharma sumitp...@gmail.com 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 a...@ashleysheridan.co.uk Date: Wed, Sep 9, 2009 at 9:14 PM Subject: Re: [PHP] Re: Class variable value lost To: Shawn McKenzie nos...@mckenzies.net Cc: Sumit Sharma sumitp...@gmail.com, 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
Re: Fwd: [PHP] Re: Class variable value lost
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
Re: [PHP] Re: Class variable value lost
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) 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