Keeping Objects "Alive"

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
brentconey
Forum Newbie
Posts: 2
Joined: Sun Aug 23, 2009 9:15 pm

Keeping Objects "Alive"

Post by brentconey »

Hello Everyone,
I just joined this forum and was looking for some help with OOP in PHP.
I know all about OOP (java) and I use it in most php projects only for a database class. The question I have about OO php is do you guys use objects for everything?
If so, do you instantiate your objects and use them all in the same page? I guess what I am asking is, is there a good method for handing off objects from page to page?
For example, If I have a multiple page web app that, just for simple sake, gets information about a user. If I wanted to store all the information in an object just updating the object from page to page is there a simple way to do this? or Do you just create the object then use it and destroy it all on the same page?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Keeping Objects "Alive"

Post by Christopher »

Generally in PHP you create everything every request. That is probably the hardest concept for programmers from other languages to grasp. Embrace it and PHP will start to make sense. And yes, most professional PHP programmers code all OOP.

That said, you can store objects in various persistent ways (they are serialized) such as in the user's session or in subsystem like memcache. But those are really optimizations that should not be done until you have an actual performance problem. Note also, that you cannot store things like database connection ID in these objects because such connections are closed when a script exits. So some of the things you are thinking of doing, that are done in long running programs, are not applicable in PHP.
(#10850)
brentconey
Forum Newbie
Posts: 2
Joined: Sun Aug 23, 2009 9:15 pm

Re: Keeping Objects "Alive"

Post by brentconey »

Thanks for the reply.
Like I said I understand using my database class that I have, and I know that it is gone when the script exits. I have a database class that has all my functions set up for queries updates, etc. I guess I just don't see the point in using objects for most things that I do with my web apps. Right now I just use sessions to hold variables that I need to keep up with.

My confusion is, is it better to make the things I'm holding in my sessions to be objects? or just keep using sessions?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Keeping Objects "Alive"

Post by Christopher »

brentconey wrote:I guess I just don't see the point in using objects for most things that I do with my web apps. Right now I just use sessions to hold variables that I need to keep up with.
You should use objects because it is a good programming practice. It would be premature optimization to resort to some worse code design for performance. Especially, when you do not yet have a performance problem (nor have solved that problem with good design practices)
brentconey wrote:My confusion is, is it better to make the things I'm holding in my sessions to be objects? or just keep using sessions?
Well ... they are both "using sessions" -- which just serializes variables and stores them in some datasource by ID. Whatever makes sense with your design is the right thing to store in the session.
(#10850)
Post Reply