Jump to content

Should an API class be a singleton?


Andy-H

Recommended Posts

I am writing an API which will handle incoming requests via the __call method, I can't currently think of any situations where multiple instances of an(y) API would be called, just wondered if someone could enlighten me as to weather this is a reasonable statement, or weather I should not make my API class a singleton, any reasons for/against?

 

 

Also, I have made a Factory that selects which database to use,

 

 


namespace \factory;


class Database
{
   private static $_db = false;
public static function getDB()
{
      if ( self::$_db )
         return self::$_db;
	switch( strtolower(DB_TYPE) )
	{
		default:
			self::$_db = new \dbh\MySQL(DB_HOST, DB_USER, DB_PASS, DB_NAME);
		break;
	}
      return self::$_db;
}
   // prevent instances being created using the new operator
   private function __construct();
   // prevent instances being cloned
   private function __clone();
   // prevent instances being unserialized
   private function __wakeup();
}

Should my MySQL class also be a singleton? As long as it's always called via the factory class, it will only be instantiated once, and the database class can be reused in other projects that may require multiple database connections. Is this the right way to go.

 

 

One last thing, lol

Is there anything I should keep in mind for future-proofing? It will be hosted on the cloud, and if it gets anywhere we will need mysql clusters and load balanced servers (DNS round-robin or something, suggestions?), do I need to develop in any certain way to prepare for this or is this all handled before the PHP script is called?

 

 

Thanks, Andy

Link to comment
Share on other sites

It currently uses the $this operator, but as far as I know at the moment, it's just going to be called with a few params, interact with a database, then throw a custom exception on __destruct containing an array to be formatted into either json or xml then outputted, I think that's all it's ever going to be used for.

 

 

To be honest though, I don't think I should make it into a singleton, perhaps just make the clone and wakeup methods private? I doubt anyone will be stupid enough to construct two api objects anyway lol

Link to comment
Share on other sites

Does the API need to remember anything, or hold anything within internal variables (state)?  Is it used directly by other objects, as in:

 

class SomeObject
{
   function someMethod($api) {
      $api->doSomething();
   }
}

 

?

 

A singleton, static class, or dependency injection are all options, but I can't recommend one over the other without knowing how you intend to use it.

Link to comment
Share on other sites

No I don't think the API needs to remember anything, it's like a main method in C or Java, it's basically where the application runs, it will require other objects but won't be called from within another object, it doesn't need to remember anything between requests - but it does have private variables such as $_API_key, $_userID, $_messages[] etc.

Link to comment
Share on other sites

OK thanks, I've not heard the term front controller but will look into it, is it a design pattern?

 

 

On the subject of PDO, are the query languages the same for common SQL engines like MySQL, PostgreSQL, SQLite etc?

Link to comment
Share on other sites

OK thanks, I've not heard the term front controller but will look into it, is it a design pattern?

 

Yup.  And, really, if you're going to do anything with OOP, you should learn the basic patterns.

 

 

On the subject of PDO, are the query languages the same for common SQL engines like MySQL, PostgreSQL, SQLite etc?

 

Basic SQL is more or less the same across db engines.  That said, they all have their own unique spins on things.  No two are exactly alike.  But, a SELECT is a SELECT.  Just be sure to write as little db engine dependent code as possible.

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.