JustinK101 Posted June 1, 2009 Share Posted June 1, 2009 Hello, I am starting a rather large project and looking for the best way to design an object oriented database query class. This is NOT a database abstraction layer, but simply a class which contains all the actual queries in a central class for organization purposes. I am fairly sure I actually don't need an instance of the class, since its basically just a wrapper, so I am thinking of three possible ways: 1.) Abstract Class EX abstract class DatabaseQueries { public function get_users() { $sql = "SELECT ...."; //Etc Etc } public function delete_user() { $sql = "UPDATE...."; //Etc Etc } } //Usage DatabaseQueries::get_users(); 2.) Make Every Method Static EX class DatabaseQueries { public static function get_users() { $sql = "SELECT ...."; //Etc Etc } public static function delete_user() { $sql = "UPDATE...."; //Etc Etc } } //Usage DatabaseQueries::get_users(); 3.) Singleton Class EX class DatabaseQueries { private static $instance; private function __construct() {} public static function singleton() { if(self::$instance == null) { self::$instance = new self; } return (self::$instance); } public function get_users() { $sql = "SELECT ...."; //Etc Etc } public function delete_user() { $sql = "UPDATE...."; //Etc Etc } } //Usage DatabaseQueries::singleton()->get_users(); Which of the three methods above is the best for scalability, performance, and just outright proper object oriented design? Or, is there a better way of doing this? Thanks a lot. Quote Link to comment Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 You can go with Singleton class. It will prevent creating multiple instances inside of a script. Do not forget to prevent cloning of the class. Thanks. Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted June 1, 2009 Author Share Posted June 1, 2009 Anupamsaha, Thanks for the reply. Wouldn't abstract and static also prevent creating multiple instances, because they by definition don't have instances? Quote Link to comment Share on other sites More sharing options...
anupamsaha Posted June 1, 2009 Share Posted June 1, 2009 Anupamsaha, Thanks for the reply. Wouldn't abstract and static also prevent creating multiple instances, because they by definition don't have instances? Well, Static classes have no instance level variables. They are best suited for utility type classes where you can group like functions together - like a String utility class. Or they are good if you want to have them as an entry point for a factory type of approach where they take care of generating the items. Singletons and Statics both provide a "global" type of entry point in terms of referencing them, but a singleton is more effective if you need to keep track of stateful items. I also asked the same question to a bunch of people and the responses were varied. Somebody had proposed the idea of stateless vs stateful as a distinction between the two. Static classes, although they can retain information about state are better off retaining information that is stateless. Singletons are better of taking care of information that is stateful. Of course, all this is just paraphasing. I hope that the article http://www.devshed.com/c/a/PHP/The-Singleton-and-Factory-Patterns-in-PHP-Building-objectoriented-forms/ will help you a lot. Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted June 2, 2009 Author Share Posted June 2, 2009 Anupamsaha, Thanks again for the reply. I guess I just don't quite understand the benefit of using a singleton in my case when using all static methods, or an abstract class will work just as well and provides for simpler code. Is there a big gotcha I am missing here? Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted June 2, 2009 Share Posted June 2, 2009 Here's how I see it: Use a Singleton if you want to have at maximum 1 instance of the class at any given moment. If your Singleton's constructor is empty then you really don't need the class to be instantiated at all, in which case you would use a static class (just remove the getInstance method) Abstract classes are a whole different thing. You would use an Abstract class if your class is part of a class hierarchy and you have an abstract parent and concrete children that have similar properties and methods but may also have different implementations of a certain method. If there is no defined hierarchy then you should use interfaces instead. Hope I helped instead of confusing more! Alex Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 2, 2009 Share Posted June 2, 2009 When using ALL static methods, you don't need singletons. And I don't understand what you mean by "an abstract class will work just as well and provides for simpler code". Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 2, 2009 Share Posted June 2, 2009 An abstract class is not an option. An abstract class is basically a skeleton for a derived class. You can't directly instantiate an abstract class. It does sound like you want a singleton pattern. You can do this either by creating it statically or instantiating an object that enforces the singleton pattern. I don't think it matters too much which you utilize. With that said, MVC is a very popular pattern, and if you were to pursue an MVC pattern, then there would be very little reason to create something like that, since your queries would live in the models and be tightly bound the the related data. Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted June 2, 2009 Author Share Posted June 2, 2009 So here is a quick example of each of the three approaches, each works as expected, but again leading back to the original dilemma, which is the best method for a class that DOES'NT have a hierarchy. Basically just an organization of database queries that I want to be able to reference in a class scope all bundle together nicely. Abstract abstract class cAbstract { public function test() { echo "abstract :: in test"; } public function test2() { echo "abstract :: in test2"; } } Static class cStatic { public static function test() { echo "static :: in test"; } public static function test2() { echo "static :: in test2"; } } Singleton class cSingleton { private static $instance; private function __construct() {} public static function singleton() { if(self::$instance == null) { self::$instance = new self; } return (self::$instance); } public function test() { echo "singleton :: in test"; } public function test2() { echo "singleton :: in test2"; } } Usage require_once("cStatic.php"); require_once("cAbstract.php"); require_once("cSingleton.php"); cStatic::test(); cStatic::test2(); cAbstract::test(); cAbstract::test2(); cSingleton::singleton()->test(); cSingleton::singleton()->test2(); Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 2, 2009 Share Posted June 2, 2009 Uh... if I say Singleton, it'll have Static included, right? In that case, go for the 2 for 1 deal! Quote Link to comment Share on other sites More sharing options...
JustinK101 Posted June 2, 2009 Author Share Posted June 2, 2009 I think I want Static, since I am able to do everything I need, including if required, having class member variables that can be read and changed, for example: Class class cStatic { private static $classvar = "classvar"; public static function test() { echo "<br>static :: " . cStatic::$classvar; cStatic::$classvar = "newclassvar"; cStatic::test2(); } public static function test2() { echo "<br>static :: " . cStatic::$classvar; } } Usage require_once("cStatic.php"); cStatic::test(); cStatic::test(); Output static :: classvar static :: newclassvar static :: newclassvar static :: newclassvar We leads me down another path of confusion, why in web applications is there a need to instantiate classes in PHP? Since PHP is stateless, seems like static methods and properties should do the trick. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 2, 2009 Share Posted June 2, 2009 It all boils down to what you're doing and how you want to represent data. Say I want to create 10 Users with name, age, and gender. How would you represent that in a Static-only world? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.