Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

What is constructor in PHP?

 

A constructor is a special member function a class which automatically initializes the properties and objects of the classes. A constructor is called automatically whenever you create an object of a class. And a constructor is used to set the default values for the members of an object. What is a constructor in oops is almost same in PHP also.

How constructors are different from a normal member function or concrete member function?
Why constructor is special in OOPS Because the constructors have the following behaviour
1. Constructor always has the same name as of class
2. The constructor will never have any return type
3. A constructor is automatically called the whenever the object of the class is initialized
4. The class will have default constructor in case if you haven’t mentioned any constructor in your class

If you haven’t understood yet property, then take a real-time example.
Consider you are developing a user module that has a different user access level

class User 
{
   protected $userType=false;
   function __construct($userType) 
   {
      $this->userType=$userType;
      echo $this->userType." level access has been created...\n";
   }
}

// // In user constructor
$obj = new User("Admin");
$obj = new User("Editor");

// output
//Admin level access has been created...
//Editor level access has been created...

 

What are the types of constructors in PHP?
In PHP there are 3 type constructor as follows
Default constructor: if __construct() method is missing in the class then default constructor will be initialized implicitly
Parameterized constructor: __construct that receives as argument
Copy Constructor: Allows to create a new object by cloning an already created object

Default constructor

class User 
{
   protected $userType="Default";
   function __construct() 
   { 
     echo $this->userType." level access has been created...\n";
   }
}

// // In User constructor
$obj = new User();
$obj = new User();

// output
//Default level access has been created...
//Default level access has been created...

 

Parameterized constructor:

class User 
{
   protected $userType=false;
   function __construct($userType) 
   {
      $this->userType=$userType;
      echo $this->userType." level access has been created...\n";
   }
}

// // In User constructor
$obj = new User("Admin");
$obj = new User("Editor");

// output
//Admin level access has been created...
//Editor level access has been created...

 

Copy Constructor:

class User 
{
   protected $userType="Default";
  function __construct() 
  {
    echo $this->userType." level access has been created...\n";
  }
  function getUserType()
  {
    return $this->userType;
  }
}

// // In User constructor
$obj = new User();
$objCopied=clone $obj;
echo $objCopied->getUserType();

// output
//Default level access has been created...
//Default