Page 1 of 1

Storing instances of classes and retrieving later...

Posted: Thu Jun 14, 2007 7:27 am
by super_soup
Hi,

Relly quick (hopefully) question....

I'm pretty new to the OO side of PHP, and have experience of JSF.

I have written php before, and used session variables to store info from forms and called those variables back out of the session when required.

When I create an instance of a class, where is it 'stored' and how do I reference it?

The way I am understanding it is that I write classes in php files, and create an instance of a class using __autoload() magic function. However, I do not understand what happens to that instance of the class!

1. If, for example I was on login.php and wanted to use the class person to store all the pertinent info about the user based on their username, do I need to explicitly tell the application to store it e.g. in the session, or is this done automatically?

2. To display that info later, for example on a page amend_details.php, how do I get that instance of the class to use the get/set methods to display/amend user details of that instance?

If you would require any more info, please ask.

Thank you.
John

Posted: Thu Jun 14, 2007 7:40 am
by Begby
PHP is stateless. That means that every time a page is retrieved from the server the script runs, then it exits completely, so nothing is stored in memory and all of your class instances disappear.

When using objects, there are a few ways to handle this. You can recreate the object on each page load by getting its properties from a db or session store, or store the object in a session by serializing them.

__autoload() is not used to load classes, instead that magic function is to load the file that a class is defined in. You can manually include your class files if you want and skip __autoload(). Your instances of classes are created with the new keyword.


So what you need to do is create an instance of your class, then store the class into a session variable using serialization, or create a method in your class to dump its properties to an array that you put in a session then a second method to recreate the class from that dumped array. On each page load you will need to restore your object.

Personally I prefer to use the latter method since serializing more complex classes can sometimes be an issue especially with nested classes, or if you change your class definition and then you have serialized classes sitting in the session store.


All of this might sound super inefficient but it isn't. Well written classes will run quickly even if you have to recreate your objects on each page load.

Posted: Thu Jun 14, 2007 3:38 pm
by Christopher
You can just assign you object into the $_SESSION array. Obviously you need to have sessions working. The only caveat is that you need to have included the class of the object that was saved before accessing it. You can use __autoload() to assist with this or include manually where you will access the object from the session.

Posted: Thu Jun 14, 2007 3:52 pm
by superdezign
I find it's nice to keep a file that you include in every file to handle that sort of thing.

Code: Select all

include_once('session.php');
include_once('user.php');

$pSession = new CSession();
$pUser = $pSession->Get('pUser');

if(!$pUser instanceof CUser)
{
    $pUser = NULL;
}

Posted: Fri Jun 15, 2007 2:51 am
by Maugrim_The_Reaper
Just to note, Sessions are temporary storage. To store user data across many days, weeks, years, you will need to use a database. Using a database is extremely easy in PHP - just make sure you escape all the data being stored to avoid SQL Injections ;).

Secondly, __autoload() can help load a classfile. But objects in PHP need to be created using the "new" keyword.

Code: Select all

$user = new User();
$user is now a variable referencing the created object. There are a few methods for allowing such objects persist themselves to a database more simply. You can look up some design patterns like Data Mapper or Active Record for a few popular methods.

Posted: Fri Jun 15, 2007 6:18 am
by kyberfabrikken
Maugrim_The_Reaper wrote:Just to note, Sessions are temporary storage.
That's an important point. In PHP - unlike in Java/JSP - you generally persist data between requests in the database. Sessions are just used for storing simple information (Such as logged-in state). Sessions are file-based, so they can't contain resources, and are generally quite slow (Often slower than the db). While this may seem awkward, it's actually a major benefit of PHP, since it guides you to write stateless applications (And that's a good thing).

Posted: Fri Jun 15, 2007 8:11 am
by feyd
kyberfabrikken wrote:Sessions are file-based
Sessions are by default file-based. You can alter that with session_set_save_handler().