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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paraleadogg
Forum Newbie
Posts: 11
Joined: Wed Feb 11, 2009 12:16 pm

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

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

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

Post 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.
(#10850)
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

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

Post by pcoder »

What about object serialization in PHP.
Post Reply