Skip to main content
8 events
when toggle format what by license comment
Nov 27, 2015 at 19:56 comment added rgvcorley Singleton is not the only reason to have a private constructor. Sometimes it is nice to be able to disable the constructor by making it private and create one or more named constructors which you do by creating a public static method which intializes an instance and returns it
Aug 9, 2015 at 0:53 review Suggested edits
Aug 9, 2015 at 1:53
Nov 27, 2014 at 5:02 comment added Neocortex So, What is the solution?
Jan 4, 2010 at 6:19 comment added Charles Private constructors are often used by PHP implementations of the Singleton pattern, and are sometimes used with Factories. For example, a factory static method of the class can search for locally cached instances of objects and return a reference instead of a new object. Making the constructor private will prevent any accidental bypasses of the factory method.
Jan 4, 2010 at 6:17 comment added zombat The only reason to do it is if you don't wish the class to be instantiated for some reason. As I mentioned in the answer, the Singleton pattern is a popular reason you might do this (en.wikipedia.org/wiki/Singleton_pattern).
Jan 4, 2010 at 6:15 comment added Brian That worked, I think I've seen people declaring private constructors before. Is there any reason to do this?
Jan 4, 2010 at 6:14 vote accept Brian
Jan 4, 2010 at 6:04 history answered zombat CC BY-SA 2.5