PHP Constructor and Destructor

If the user wants to access any member function of a class then he or she must create the object first then call the member function by this object.

The constructor is a method or way which automatically called when an object the for a class.

No need to call it by an object. Simply constructor is a function which called when object creates of class.

Constructor declares by two ways.

  1. Define function name same as class name. (This is old way to declare constructor. It is use in PHP 5.3.3 version)
  2. Define function by __construct keyword.

Only one constructor creates in one class. It is used to run an operation which needs to run at first of any other operation.

Like Database Connection. The constructor

Example:

Output:

Explanation:

In above example $dell is an object of Computer class when its created constructor automatically called and CPU, RAM and Hard disk value passed by parameter.

Then when called describe() function its return the whole value

Destructor

the is also a method or way in OOPS which is opposite of Constructor.

Destructor also called automatically when object an of any class but it’s run when The constructor creates.

It the for free up memory. Using this after complete a task unset all variable and re The constructor it in next program.

Destructor defines by __destruct keyword. an is also a function which automatically called when object The constructor.

Example:

Output

Constructors in Java

A Constructor in a special method that is used to initialize the object.

Constructor name should be same as class name. Constructors does not have any return type.

While creating object of above class we use

Here new is used to create object and after then the constructor Student() is get called it provide initial value to object

When an object is created using new operator then to initialize the object constructor is called immediately.

A class can contain more than one constructor.  If a class does The constructor the constructor, the system provides the default constructor which is known as system default constructor resides.

The default constructor  automatically initialize all instance variable to zero

Characteristics of Constructor

  • The constructor has the same name as the class in which it resides.                        
  • The constructor is automatically called immediately after the object of that class created in which it resides.
  • Constructor neither returns value nor void.    
  • Access specifiers public protected,default and private can be used with constructors.                                                                         
  • The constructor can be overloaded.
  • A constructor can also call another constructor.                                                 
  • Default constructor automatically initialize all instance variable to zero  
  • Constructors can not be abstract, final ,native synchronized or static.

Types of Constructors

In Java Constructors are categorized in three types

  • Default (No Argument) Constructor
  • Parameterized Constructor

Default (No Argument) Constructor

A default constructor in no argument constructor it initialize object value to initial value.

In above program no argument constructor in provided student() on executing above program it will show following result

Result

If a java programmer has written a program and didn’t provided any constructor in program in that case during compile time compiler will include a no argument constructor.

Compiler will only include this constructor it there is no constructor available in program.

If there is already a parameterised constructor and not a no argument constructor then compiler will not include default constructor it will raise compile a time error.

Program to show use of constructor

No constructor provided in class Student. while creating object we are calling Student() (default constructor) of Student class.

The default constructor is provided by the compiler.

Constructor with Arguments

In Java, we can create a create a constructor which can take zero, one or more than arguments.

Result

Note: In the above program we have created class Student and three constructors  Student(), Student(long r) and Student(long r, String n ).

When the line Student obj1 = new Student(); executes and immediately after the object “obj1” is create, Student() constructor will be called. When The constructor line Student obj2 = new Student (1); executes and immediately after the object “obj2” is create, Student(long r) constructor will be called and passes the value 1 to the constructor. When line “ Student obj3 = new Student(1,"Ram"); executes and immediately after the object “obj3” is create, Student(long r, String n) constructor will be called and passes the value 1 and “Ram” to the constructor.