Static class 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
greetification
Forum Newbie
Posts: 4
Joined: Thu Apr 22, 2010 3:19 pm

Static class question

Post by greetification »

Hi, I was wondering, can a static class take the place of a session?

For instance, if i have a static class that access a DB and stores the result in a static var, once that class has been instantiated how long will it be available?

Basically I want to only access the DB once and store the info so I don't have to query the DB every time I need the same info.

Thanks!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Static class question

Post by John Cartwright »

PHP is a stateless paridigm. Like most things in PHP, they only live throughout the lifespan of the request. I.e., once the request is completed, the memory is dumped.

Sessions are a perfectly acceptable solution for this.
greetification
Forum Newbie
Posts: 4
Joined: Thu Apr 22, 2010 3:19 pm

Re: Static class question

Post by greetification »

Thanks John,

I'm working on my first php oop project (trying to utilize my AS3 oop knowledge) so i'm still trying to get the hang of things.

So if it is stateless, what is the point of setting anything as static, is it only if you don't want to use a constructor?

Thanks!
Post Reply