What are constructor and destructor in PHP

The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we solve previous year question papers for you.
In eduladder you can Ask,Answer,Listen,Earn and Download Questions and Question papers.
Watch related videos of your favorite subject.
Connect with students from different parts of the world.
Apply or Post Jobs, Courses ,Internships and Volunteering opportunity. For FREE
See Our team
Wondering how we keep quality?
Got unsolved questions? Ask Questions

PHP-->View question

What are constructor and destructor in PHP?

its about What are constructor and destructor in PHP


Asked On2019-09-06 15:46:36 by:Annaliya

Taged users:


Likes:
Be first to like this question

Dislikes:
Be first to dislike this question
Talk about this  Like  Dislike
View all qusetions
Answers

When we create an object of any class, we need to set properties of that object before using it. We can do that by first initialising the object and then setting values for the properties, either by using the -> operator if the variables are public, or using the public setter methods for the private variables.

To create and initialize a class object in a single step, PHP provides a special method called as Constructor, which is used to construct the object by assigning the required property values while creating the object.

And for destroying the object Destructor method is used.


Syntax for defining Constructor and Destructor

In PHP, we have special functions to define constructor and destructor for a class, they are: __construct() and __destruct().

<?php
    class <CLASS_NAME> {
        
        // constructor
        function __construct() {
            // initialize the object properties
        }
        
        // destructor
        function __destruct() {
            // clearing the object reference
        }
    }
?>

Constructor can accept arguments, whereas destructors won't have any argument because a destructor's job is to detroy the current object reference.


PHP Constructor

Let's take the example of a class Person which has two properties, fname and lname, for this class we will define a constructor for initialising the class properties(variables) at the time of object creation.

<?php
    class Person {
        // first name of person
        private $fname;
        // last name of person
        private $lname;
        
        // Constructor
        public function __construct($fname, $lname) {
            echo "Initialising the object...<br/>"; 
            $this->fname = $fname;
            $this->lname = $lname;
        }
        
        // public method to show name
        public function showName() {
            echo "My name is: " . $this->fname . " " . $this->lname; 
        }
    }
    
    // creating class object
    $john = new Person("John", "Wick");
    $john->showName();
    
?>

Initialising the object... My name is John Wick


Answerd on:2019-09-07 Answerd By:Tanisha-Garg

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

You might like this video:Network and IP Address
Watch more here

Watch more videos from this user Here

Learn how to upload a video and start earning here



Lets together make the web is a better place

We made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams" with these.
Answer a question: You can answer the questions not yet answered in eduladder.How to answer a question
Career: Work or do your internship with us.Work with us
Create a video: You can teach anything and everything each video should be less than five minutes should cover the idea less than five min.How to upload a video on eduladder