Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When we should make the constructor Private & Why? PHP [duplicate]

Possible Duplicate:
In a PHP5 class, when does a private constructor get called?

I have been reading about OOP recently and came across this private constructor scenario. I did a Google search, but couldn't find anything relevant to PHP.

In PHP

  • When do we have to define a private constructor?
  • What's the purpose of using a private constructor?
  • What are the pros & cons of using a private constructor?
like image 285
Techie Avatar asked Sep 23 '12 14:09

Techie


People also ask

When should I make a constructor private?

Private constructors are used to prevent creating instances of a class when there are no instance fields or methods, such as the Math class, or when a method is called to obtain an instance of a class. If all the methods in the class are static, consider making the complete class static.

What if we make constructor private?

A private constructor in Java is used in restricting object creation. It is a special instance constructor used in static member-only classes. If a constructor is declared as private, then its objects are only accessible from within the declared class. You cannot access its objects from outside the constructor class.

Should a constructor be private?

No, Constructors can be public , private , protected or default (no access modifier at all). Making something private doesn't mean nobody can access it. It just means that nobody outside the class can access it. So private constructor is useful too.

When would you use a private constructor in Java?

Private constructors allow us to restrict the instantiation of a class. Simply put, they prevent the creation of class instances in any place other than the class itself. Public and private constructors, used together, allow control over how we wish to instantiate our classes – this is known as constructor delegation.


1 Answers

There are several scenarios in which you might want to make your constructor private. The common reason is that in some cases, you don't want outside code to call your constructor directly, but force it to use another method to get an instance of your class.

Singleton pattern

You only ever want a single instance of your class to exist:

class Singleton {     private static $instance = null;      private function __construct()     {     }      public static function getInstance()     {         if (self::$instance === null) {             self::$instance = new self();         }          return self::$instance;     } } 

Factory method

You want to provide several methods for creating an instance of your class, and/or you want to control the way your instances are created, because some internal knowledge of the constructor is needed to properly call it:

class Decimal {     private $value; // constraint: a non-empty string of digits     private $scale; // constraint: an integer >= 0      private function __construct($value, $scale = 0)     {         // Value and scale are expected to be validated here.         // Because the constructor is private, it can only be called from within the class,         // so we can avoid to perform validation at this step, and just trust the caller.          $this->value = $value;         $this->scale = $scale;     }      public static function zero()     {         return new self('0');     }      public static function fromString($string)     {         // Perform sanity checks on the string, and compute the value & scale          // ...          return new self($value, $scale);     } } 

Simplified example from the BigDecimal implementation of brick/math

like image 152
BenMorel Avatar answered Oct 17 '22 17:10

BenMorel