Andy-H Posted January 6, 2012 Share Posted January 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/ Share on other sites More sharing options...
KevinM1 Posted January 6, 2012 Share Posted January 6, 2012 RE: your database - why not simply use or extend PDO as your abstraction layer? You're attempting to recreate something that already exists within the language. Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1304939 Share on other sites More sharing options...
Andy-H Posted January 6, 2012 Author Share Posted January 6, 2012 Yeah that's a good point, I suppose I just forgot what PDO was and was thinking of it as a prepared-statement emulator for MySQL lol, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1304941 Share on other sites More sharing options...
KevinM1 Posted January 6, 2012 Share Posted January 6, 2012 RE: your API class - are you going to be retaining any state? Do you need an instance? Is it passed around or used anywhere else? Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1304945 Share on other sites More sharing options...
Andy-H Posted January 6, 2012 Author Share Posted January 6, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1304949 Share on other sites More sharing options...
KevinM1 Posted January 6, 2012 Share Posted January 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1304956 Share on other sites More sharing options...
Andy-H Posted January 6, 2012 Author Share Posted January 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1304958 Share on other sites More sharing options...
KevinM1 Posted January 6, 2012 Share Posted January 6, 2012 Sounds like a front controller. You don't need a singleton or static class. Since this class lies at the top of your app, and simply dispatches to other objects, a normal instance would work just fine. Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1304959 Share on other sites More sharing options...
Andy-H Posted January 6, 2012 Author Share Posted January 6, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1304976 Share on other sites More sharing options...
KevinM1 Posted January 6, 2012 Share Posted January 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1304990 Share on other sites More sharing options...
Andy-H Posted January 6, 2012 Author Share Posted January 6, 2012 Ok, thanks for all your help Quote Link to comment https://forums.phpfreaks.com/topic/254492-should-an-api-class-be-a-singleton/#findComment-1305015 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.