Constructors & Destructors

This lesson introduces the concept of constructors, destructors, how to declare and call them.

Introduction

A constructor is used to initialize member variables when an object is declared. It is automatically called at the time when the object of the class is declared.

Note: A constructor is a member function that is usually public.

Also keep in mind that unlike other methods defined inside a class, a constructor cannot return a value.

Constructor Declaration

Classes can define a special __construct() method, which is executed as part of object creation.

Let’s take a look at how it is defined:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy