Awilum Posted January 29, 2012 Share Posted January 29, 2012 I write for example class Text: class Text { public static function test() { return 'test text'; } } Today I found this code: class Text { /** * Protected constructor since this is a static class. * * @access protected */ protected function __construct(){ // Nothing here } public static function test() { return 'test text'; } } How much appropriate is to write this? /** * Protected constructor since this is a static class. * * @access protected */ protected function __construct(){ // Nothing here } Quote Link to comment https://forums.phpfreaks.com/topic/256005-static-class/ Share on other sites More sharing options...
AyKay47 Posted January 30, 2012 Share Posted January 30, 2012 The purpose of a private or protected constructor is to prevent the class from being instantiated from outside of the class. It can give you control over how the constructor() method is instantiated. In this case it would defer statically calling the constructor. But of course with this small snippet of a class, it doesn't make any difference. Quote Link to comment https://forums.phpfreaks.com/topic/256005-static-class/#findComment-1312435 Share on other sites More sharing options...
scootstah Posted January 30, 2012 Share Posted January 30, 2012 For the record, this is called the singleton pattern. It works like this class Singleton { public static $instance; private function __construct() { } public static function getInstance() { if (!self::$instance) self::$instance = new Singleton; return self::$instance; } } $singleton = Singleton::getInstance(); Basically, you make the constructor static which prevents it from being instantiated outside of the class. Then, by calling the getInstance method, you instantiate the class internally and then return it. Since the $instance variable is static, this can only happen once. If you call the getInstance method again it simply returns the instance instead of instantiating it multiple times. Quote Link to comment https://forums.phpfreaks.com/topic/256005-static-class/#findComment-1312466 Share on other sites More sharing options...
KevinM1 Posted January 30, 2012 Share Posted January 30, 2012 Slight quibble - a Singleton is defined by the fact that it has a static method return (or create THEN return) an instance of its own class. Merely having a protected/private constructor in a class with static methods does NOT make it a Singleton (nor imply that intent). Static utility classes that don't need to retain state/utilize an instance is a common design pattern, to the extent that at least one language (C#) allows one to define an entire class as static, which forces the class itself to ONLY contain static methods. Regardless, static classes and Singletons should be used with care as they ignore scope and encapsulation. There's usually a better way to solve a problem than to use one of them. Quote Link to comment https://forums.phpfreaks.com/topic/256005-static-class/#findComment-1312472 Share on other sites More sharing options...
scootstah Posted January 30, 2012 Share Posted January 30, 2012 Slight quibble - a Singleton is defined by the fact that it has a static method return (or create THEN return) an instance of its own class. Merely having a protected/private constructor in a class with static methods does NOT make it a Singleton (nor imply that intent). True. Thanks for pointing that out. Quote Link to comment https://forums.phpfreaks.com/topic/256005-static-class/#findComment-1312475 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.