Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Constructor and Destructor
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. PHP Object Oriented Tutorials
  3. Constructor and Destructor

PHP Object Oriented Tutorial

Basic Object Oriented Tutorials

  • Object Oriented Programming In PHP
  • Advantage of OOPS In PHP
  • Important OOPS concepts
  • Oops Features In PHP
  • Class In PHP
  • Object In PHP
  • Constructor and Destructor
  • Inheritance in PHP
  • Encapsulation In Php
  • Abstract Classes In PHP
  • Interface In PHP
  • Polymorphism In Php

Constructor and Destructor

What is Constructor

The Constructor is a class function called automatically when an instance is created for that class. The Destructor is a function that is invoked when the class instance is destroyed. In PHP, the constructor and the destructor are defined by using the magic functions __construct() and __destruct() respectively. The following code shows an example with the class constructor used to initialize its private property.

Constructor types:

  • Default Constructor: It has no parameters, but the values to the default constructor can be passed dynamically.
  • Parameterized Constructor: It takes the parameters, and also you can pass different values to the data members.
  • Copy Constructor: It accepts the address of the other objects as a parameter.

 

Example Of Constructor

 

<?php
class Car{
    private $modal;
    function __construct() {
        $this->modal = array("Alto","I10", "Audi");
    }
    function getAllModal() {
        foreach($this->modal as $modal) {
            print $modal . "<br/>";
        }
    } 
}

$carObj= new Car();
echo "<b>All Modals:</b><br/>";
$carObj->getAllModal();
?>

 

What is Destructor: 

Destructor is also a special member function which is exactly the reverse of the constructor method and is called when an instance of the class is deleted from the memory. 

 

Example Of Destructor

 

<?php
class Car{
    private $modal;
    function __construct() {
        $this->modal = array("Alto","I10", "Audi");
    }
    function getAllModal() {
        foreach($this->modal as $modal) {
            print $modal . "<br/>";
        }
    } 
    function __destruct(){ 
      echo "destroying " . $this->name . "\n"; 
    } 
}

$carObj= new Car();
echo "<b>All Modals:</b><br/>";
$carObj->getAllModal();
?>
Output:
Alto
In the constructor, destroying Class object! 

 

Series: PHP Object Oriented Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
What is Constructor What is Destructor

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.