Storing instances of classes and retrieving later...

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
super_soup
Forum Newbie
Posts: 1
Joined: Thu Jun 14, 2007 5:35 am

Storing instances of classes and retrieving later...

Post 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
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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;
}
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
kyberfabrikken
Forum Commoner
Posts: 84
Joined: Tue Jul 20, 2004 10:27 am

Post 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).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

kyberfabrikken wrote:Sessions are file-based
Sessions are by default file-based. You can alter that with session_set_save_handler().
Post Reply