Skip to content
CS Chetan Patil
About Projects Blog Gallery Contact
Resume
  1. Home
  2. Blog
  3. OOP in PHP

OOP in PHP 20 January 2017 · 2 min read

Constructor

What does Constructor mean? A constructor is a special method of a class or structure in OOP that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be…

Chetan S. Patil Software Engineer at Genpact

What does Constructor mean?

A constructor is a special method of a class or structure in OOP that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values.

Constructors are not called explicitly and are invoked only once during their lifetime. In the case of a hierarchy of classes where a derived class inherits from a parent class, the execution sequence of the constructor is a call to the constructor of the parent class first and then that of the derived class.

Constructors cannot be inherited.

A constructor can be declared using any of the access modifiers. It is mandatory to have a constructor with the right access modifier. However, the compiler supplies a default if an access modifier is not defined in the class. If a constructor is declared as private, the class cannot be created or derived and hence cannot be instantiated. Such a constructor, however, can be overloaded with different sets of parameters. 

Constructor design:

  • When using derived class constructors, the parent class constructor should be passed the correct parameters.
  • Better code maintainability comes from having the initialization and other related logic in one main constructor and cross-calling this constructor from other overloaded constructors.
  • Logic involving specific operations that need to be executed at a particular event in an application – such as opening a database connection – should not be written in a constructor.
  • Because a constructor cannot return a value to the calling code, it is a good practice to throw an exception when a failure is encountered.
#OOP
Share this
Chetan S. Patil

Written by

Chetan S. Patil

Software Engineer in Pune — PHP, Symfony and SQL Server behind financial operations and payment systems.

About LinkedIn
PreviousDependency injection in PHP NextCheck number is Fibonacci numbers or not

Related reading

All articles

OOP in PHP · 3 min read

Dependency injection

Dependency injection is a technique where one object supplies the dependencies of another object. An injection is the passing of a dependency…

15 Jun 2017

OOP in PHP · 7 min read

Dependency injection in PHP

Dependency Injection is a software design pattern that allows avoiding hard-coding dependencies and makes possible to change the dependencies both at runtime…

12 Jul 2016

OOP in PHP · 1 min read

Differences between abstract class and interface in PHP

In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract. Multiple and…

8 Jul 2016

CS Chetan Patil

Software Engineer in Pune — PHP, Symfony and SQL Server behind financial operations and payment systems.

i@chetanpatil.co.in

Site

About Projects Blog Gallery Resume Contact RSS

Elsewhere

Pune, Maharashtra · IST (UTC+5:30)

© 2026 Chetan S. Patil

Built with PHP & MySQL

Chetan Patil
About Projects Blog Gallery Contact Resume
i@chetanpatil.co.in