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!
Not being a seasoned PHP developer, tonight I started playing with the use of the Singleton pattern to store configuration information. What I was surprised to find was that the instance - at least the one I'm creating - isn't really a singleton. With each request, the constructor is called. Isn't that very much *not* a singleton or am I misunderstanding or doing something wrong? I'm using a pretty simple case:
Everah: mm don't really see how your code changed something.
musetracks: thats normal. You are making absolutely new request. Singleton will work within same request only. Or
if you make some session hold of objects....
You will have to save the object like any other class instance you wish to store over multiple requests. Look up sessions and serializing in the documentation for examples.
Sorry, didn't read the part about page refreshing.
That makes sense. PHP is essentially stateless, so everytime you refresh a page you are invoking the script all over again. The Singleton pattern is most suited to maintaining a single instance of an object throughout the life of a request (all 0.0034 seconds of it).