RobertP Posted October 30, 2011 Share Posted October 30, 2011 so i am open to suggestions and comments <?php if(!defined('_ROOT')) exit(header('HTTP/1.0 404 Not Found')); class configuration { private static $_instance; private configuration(){ } public static function &getInstance(){ if(!self::$_instance) self::$_instance = new configuration(); return self::$_instance; } } $config = configuration::getInstance(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/250093-creating-an-oop-class/ Share on other sites More sharing options...
KevinM1 Posted October 30, 2011 Share Posted October 30, 2011 If you're using PHP 5+, you don't need the '&' in front of getInstance(). Aside from that, your class is a bare bones, textbook example/copy of a Singleton. There's nothing to critique or say, aside from the general warning about using Singletons. Quote Link to comment https://forums.phpfreaks.com/topic/250093-creating-an-oop-class/#findComment-1283484 Share on other sites More sharing options...
RobertP Posted November 1, 2011 Author Share Posted November 1, 2011 If you're using PHP 5+, you don't need the '&' in front of getInstance(). Aside from that, your class is a bare bones, textbook example/copy of a Singleton. There's nothing to critique or say, aside from the general warning about using Singletons. ty for the hint about '&' as i am using php5.3.8 and what do you mean by "general warning about using singletons" ? Quote Link to comment https://forums.phpfreaks.com/topic/250093-creating-an-oop-class/#findComment-1284087 Share on other sites More sharing options...
KevinM1 Posted November 1, 2011 Share Posted November 1, 2011 Singletons are just another global variable. Globals are bad, as they break scope and encapsulation, both of which are cornerstones of good programming in general, and OOP specifically. Usually, if you're thinking about using a Singleton, it's a sign that you haven't thought about your design properly. Not always, but usually. A lot of "Should I use a Singleton here?" questions can be solved much more eloquently by using Dependency Injection/Inversion of Control (which are two names for the same thing). Look into the Symfony Dependency Injection Container (Google it) for a general idea of what it is. Quote Link to comment https://forums.phpfreaks.com/topic/250093-creating-an-oop-class/#findComment-1284095 Share on other sites More sharing options...
markjoe Posted November 1, 2011 Share Posted November 1, 2011 I don't think I can agree that a Singleton class is equivalent to global variables. Sure they certainly can be overused, but everything can. There are plenty problems that are perfectly solved by a Singleton class. (like when pooling database connections or threads) The situations that call for a Singleton, I'd guess, should only occur once or twice in a typical application, IF at all. for the record, I think global variables are much more offensive that Singletons. Quote Link to comment https://forums.phpfreaks.com/topic/250093-creating-an-oop-class/#findComment-1284101 Share on other sites More sharing options...
KevinM1 Posted November 1, 2011 Share Posted November 1, 2011 The situations that call for a Singleton, I'd guess, should only occur once or twice in a typical application, IF at all. Usually' date=' if you're thinking about using a Singleton, it's a sign that you haven't thought about your design properly. Not always, but usually.[/quote'] I don't see much difference between these two statements. Also note that the vast, vast majority of members asking for OOP help are complete newbies. They're likely not going to be concerned about pooling database connections (and PHP doesn't have threads anyway, so that's a moot example). In my experience, beginners often attempt to use what few design patterns they successfully learn as the solution to all problems, at times even going so far as to think that an app isn't well designed unless it features at least one example of each of those patterns. I was attempting to curb that, and the potential misuse of a pattern which remains controversial specifically because it is misused so often. Singletons, as you noted, have their place. However, they should be considered the last chosen, least appealing alternative if only because they break encapsulation by their very nature. Quote Link to comment https://forums.phpfreaks.com/topic/250093-creating-an-oop-class/#findComment-1284103 Share on other sites More sharing options...
RobertP Posted November 1, 2011 Author Share Posted November 1, 2011 i am using oop classes to load my database, and configuration. i am not a fan of global's either, but i think it is much easier to use oop then a crazy global tree inside functions Quote Link to comment https://forums.phpfreaks.com/topic/250093-creating-an-oop-class/#findComment-1284105 Share on other sites More sharing options...
KevinM1 Posted November 2, 2011 Share Posted November 2, 2011 You don't need a Singleton for that. Quote Link to comment https://forums.phpfreaks.com/topic/250093-creating-an-oop-class/#findComment-1284146 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.