Page 1 of 1

OO php - quick object question........

Posted: Mon Mar 30, 2009 7:24 pm
by paraleadogg
Hi guys,

Ok - I am having a crack at OO php.

I have created some classes (for example user, item)

Firts of all on my index page I check a login (username, password). If there is a match in the database i get the id from the database and store it in a session. Then i create a new instance of my user class and call a function called "getDetails". I pass the session id to this method which then gets all the users details from the database and sets the user objects parameters - here is an example (the index page)

Code: Select all

 
require_once 'user_class.php';
 
$user = new user();
 
$user->getDetails($_SESSION['id']);
 
echo $user->firstname; // print users first name
 
Here is my question.

If i create a NEW page (items.php) how do I access my $user object from that page??

Do i have to create a new instance and then hit the database again?? Or is there a way to have an object to be universally available?? it seems a bit stupid to have to instantiate and then populate a new object on every single page?

I am pretty sure this is a dumb question but all the examples i have seen have the object created and used within scope.

I know I can pass an object to a function etc and have tried get and set methods but nothing seems to work

I bascially want to create an object then have it universally available throught the site for the duration of the session....

Any pointers, advice, critisism welcome

Thank you

Re: OO php - quick object question........

Posted: Mon Mar 30, 2009 10:29 pm
by Christopher
paraleadogg wrote:Do i have to create a new instance and then hit the database again?? Or is there a way to have an object to be universally available?? it seems a bit stupid to have to instantiate and then populate a new object on every single page?
With PHP you create everything every request ("page"). Seems wrong, ends up being brilliant.

Re: OO php - quick object question........

Posted: Mon Mar 30, 2009 11:07 pm
by pcoder
What about object serialization in PHP.