Singleton Classes in PHP: Not like other OOP singleton classes...
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Singleton Classes in PHP: Not like other OOP singleton classes...
Hi all,
I've rather naively written a singleton php database class, but now realize that it simply doesn't work because php classes are not persistent, I mean, there is no memory of anything once you've downloaded a page.
So would the solution be to use a session variable ($_SESSION) to store the unique class instance? Or is there a better, classic solution? Or perhaps I should use a simple database framework.
None of the database frameworks I know of use sessions or persist in the way you're describing. Generally you take care of sessions separately from the database layer.
As kelso.b has said you need to provide more details about what you want to achieve. Normally, a singleton class within a session is sufficient, why in particular do you need a singleton across sessions? This can be done it's just that we need to understand your specific use-case.
My application doesn't even have an interface. It is to be used with an other application which does have an interface, written in php. It is an NLP module.
It is experimental. In other words, I try things and see what happens. Sometimes, often!, I try something and because of an unending loop I have to interrupt the http request (localhost, always using the database). Because of the interruption there is often, after a while, especially when debugging, lots of db connections in progress. I know this because my Netbook slows down almost to a stand-still but when I restart MySQL, it then operates normally.
My question is also a general one: How does one go about implementing the singleton pattern in php? It just doesn't work like it does in Java or C++, for obvious reasons. And it only seems logical to me that if you only want one database connection on your machine because you only have one user, that you would use a singleton pattern. It's probably the exception to the large majority of web applications where you have to have lots of simultaneous connections.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.