LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2012, 09:31 AM   #1
rm_-rf_windows
Member
 
Registered: Jun 2007
Location: Europe
Distribution: Ubuntu
Posts: 292

Rep: Reputation: 27
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.

Thanks in advance,

rm
 
Old 04-18-2012, 06:15 PM   #2
kelso.b
LQ Newbie
 
Registered: Apr 2012
Posts: 4

Rep: Reputation: Disabled
What is your class trying to achieve?

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.
 
Old 04-18-2012, 08:53 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 04-19-2012, 10:02 AM   #4
rm_-rf_windows
Member
 
Registered: Jun 2007
Location: Europe
Distribution: Ubuntu
Posts: 292

Original Poster
Rep: Reputation: 27
kelso.b, graemef,

Sorry about the delay.

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.

Thanks in advance,

rm
 
Old 04-19-2012, 11:13 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You might want to look at persistent connections http://www.php.net/manual/en/mysqli.persistconns.php
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Class inheritance terminology: super-classes or base classes? 2ck Programming 6 07-20-2011 10:17 AM
Singleton delete instance Genar Programming 1 03-04-2007 09:34 AM
c++: Problems with a singleton (as a so) eantoranz Programming 11 02-20-2006 09:38 AM
singleton pattern in C++? Thinking Programming 7 11-13-2005 07:45 PM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration