Jump to content

Object Oriented Question, Abstract Vs Static Vs Singleton


JustinK101

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.