![]() |
Singleton PDO Database class, tips/suggestions?
Hi all,
This is a singleton PDO Database class I wrote. Do you have any tips/suggestions? PHP Code:
|
PDO is nice but I personally would go for a Singleton/Factory.
The class looks pretty nice though :) |
I've made a little update to the database class, here it is:
Again, tips and suggestions are more than welcome! PHP Code:
|
Where is
LOCAL defined and why do we have an empty __clone method? |
Quote:
As for the private __clone method, that's a common thing (I do it myself), and it's good combined with the singleton pattern. Trying to clone the object returned by the getInstance() method will be permitted (thus breaking the singleton rules), as long as you don't define the __clone method as private :-P I don't agree though with the public connect function. Calling that method several times will only recreate the connection to the DB. You'd probably want to extend the actual PDO class and transform it into a singleton from within your class. ;-) |
Thanks for the feedback. The first file which gets loaded is
init.php:PHP Code:
connect() function private. I will also add a getter/setter to the class instead of relying on a defined global.Thanks! |
What I did now instead of the global defined LOCAL variable, I made a Registry class where I save global variables. Then i pass the Registry class to every other class which might need the global variables.
In my init.php file:php Code:
And then my Database class looks like: php Code:
Any tips/suggestions on this way of doing things? |
| All times are GMT. The time now is 12:55 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0