What are the __construct() and __destruct() methods in a PHP ?
Last Updated :
23 Jan, 2023
The constructor is the OOPs concept in PHP. It is a method that has the same name as the class name. It is defined inside the class and is used to automatically call when the object is created.
PHP4 provides the constructor method whereas PHP5 provides the magic method __construct and __destruct. This method is automatically called when an object is created or destroyed. This function always starts with two underscores.
__construct() Method: __construct is a public magic method that is used to create and initialize a class object. __construct assigns some property values while creating the object. This method is automatically called when an object is created.
Properties:
- __construct is a public magic method.
- __construct is a method that must have public visibility
- __construct method can accept one and more arguments.
- __construct method is used to create an object.
- __construct method can call the class method or functions
- __construct method can call constructors of other classes also.
The constructor will initialize the class properties at the time of object creation. The __construct() method will be called only once when the object of the class is created.
Syntax:
$object_name= new class_name (argument value);
Example:
$subject=new computer(“English”);
Syntax:
function __construct() {
// Initialize the object properties
}
Approach:
- Default Constructor: By default, __construct() method has no parameters. The values passed to the default constructor are default.
- Parameterized Constructor: In parameterized constructor __construct() method takes one and more parameters. You can provide different values to the parameters.
- Copy Constructor: In the copy constructor, the __construct() method accepts the address of the other objects as a parameter.
Default Constructor: In default constructor, the __construct() method has no parameters. The values passed to the default constructor are default.
Example: Let us take the example of a class “student” that will display a simple message for this class, We will define a constructor without a parameter.
Constructor without parameter:
PHP
<?php
class Student {
function __construct() {
print "This is __construct without parameter\n" ;
print "Welcome To GeeksforGeek" ;
}
}
$obj = new Student();
?>
|
Output:
This is __construct without parameter
Welcome To GeeksforGeek
Constructor with parameter:
Example 2: Let us take another example of a class “student” who has two properties “name” and “surname“. For this class, we will define a constructor with a parameter that will initialize class properties when the object is created.
PHP
<?php
class student {
public $name ;
public $surname ;
public function __construct( $name , $surname ) {
$this ->name = $name ;
$this ->surname = $surname ;
}
public function display() {
echo "My name is " . $this ->name
. "<br>Surname is " . $this ->surname;
}
}
$user = new student( "john" , "biber" );
$user ->display();
?>
|
Output:
My name is john
Surname is biber
PHP Destructor: PHP Destructor method is used to destroy objects or release their acquired memory. A destructor is called automatically when the object is created. Usually, it is called at end of the script. The destructor method does not take any arguments, The destructor does not return any data type. This all process is handled by Garbage Collector.
Properties:
- __destruct() method does not take any parameter.
- __destruct() method will not have any return type.
- This method works exactly the opposite of the __construct method in PHP.
- __destruct gets called automatically at the end of the script.
- __destruct() method starts with two underscores (__).
- It is used to de-initialize existing objects.
Syntax:
function __destruct() {
// Destroy objects or release memory.
}
Example:
PHP
<?php
class student {
function __construct() {
echo "This is a constructor<br>" ;
echo "Object is initialized in constructor<br>" ;
}
function __destruct() {
echo "This is destruct<br>" ;
echo "Object is destroyed in destructor" ;
}
}
$subject = new student();
?>
|
Output:
This is a constructor
Object is initialized in constructor
This is destruct
Object is destroyed in destructor
Example: Now, let us take an example of a “class” student who has three properties “name”, “surname”, and “favorite website”. For this class, we will define a constructor with a parameter and the destructor will destroy the initialized object.
PHP
<?php
class Student {
public $name ;
public $surname ;
public $website ;
public function __construct( $name ,
$surname , $website ) {
$this -> name = $name ;
$this -> surname = $surname ;
$this -> website = $website ;
}
public function __destruct() {
echo "My name is {$this -> name} "
. "<br>Surname is {$this -> surname}" ;
echo "<br>My favorite website is"
. "{$this -> website}" ;
echo "<br>Successfully object Destroyed" ;
}
}
$info = new Student( "John" , "Biber" , "Geeksforgeek" );
?>
|
Output:
My name is John
Surname is Biber
My favorite website is Geeksforgeek
Successfully object Destroyed
Example: Let us create a class “MyClass”. In the constructor, we will define a new class property and destroy them in the destructor.
PHP
<?php
class MyClass {
function __construct() {
echo "You are in constructor<br>" ;
$this ->name = "MyClass Object" ;
}
function __destruct() {
echo "You are in destructor<br>" ;
print "Just Destroyed " . $this ->name;
}
}
$obj = new Myclass();
?>
|
Output:
You are in constructor
You are in destructor
Just Destroyed MyClass Object
Conclusion: In the real world, constructors and destructs are very useful as they space in memory. They allow the reusability of code. Overall they are very useful.
Similar Reads
What are the final class and final method in PHP ?
In this article, we will see what is final class & final method in PHP, along with knowing their implementation through the examples. The terms class, object, method, and final comes under object-oriented programming. In object-oriented programming, there is an important concept called Inheritan
3 min read
PHP | Constructors and Destructors
In PHP, constructors and destructors are special methods that are used in object-oriented programming (OOP). They help initialize objects when they are created and clean up resources when the object is no longer needed. These methods are part of the class lifecycle. In this article, we will discuss
5 min read
What are magic methods and how to use them in PHP ?
PHP magic methods are special methods that are called automatically when certain conditions are met. There are several magic methods in PHP. Every magic method follows certain rules - Every magic method starts with a double underscore ( __ ).They are predefined and neither can be created nor removed
4 min read
Perl | Constructors and Destructors
Constructors Constructors in Perl subroutines returns an object which is an instance of the class. In Perl, the convention is to name the constructor "new". Unlike many other OOPs, Perl does not provide any special syntax for constructing an object. It uses Data structures(hashes, arrays, scalars) t
4 min read
PHP | Ds\Set __construct() Function
The Ds\Set::__construct() function is an inbuilt function in PHP which is used to create new instance of set. Syntax: Ds\Set::__construct( $values ) Parameters: This function accepts single parameter $values which holds the traversable object or array to use initial values. Below programs illustrate
1 min read
PHP | Ds\Stack __construct() Function
The Ds\Stack::__construct() function is an inbuilt function in PHP which is used to create a new instance of stack. Syntax: public Ds\Stack::__construct( $values ) Parameters: This function accepts single parameter $values which holds the traversable object or array to use initial values. Below prog
1 min read
PHP | Ds\Vector __construct() Function
The Ds\Vector::__construct() function is an inbuilt function in PHP which is used to creates a new instance. Syntax: public Ds\Vector::__construct( $values ) Parameters: This function accepts single parameter $values which holds the traversable object or array to use initial values. Below programs i
1 min read
PHP | Ds\Map __construct() Function
The Ds\Map::__construct() function is an inbuilt function in PHP which is used to creates a new instance. Syntax: public Ds\Map::__construct( $values ) Parameter: This function accepts a single parameter $values which holds the traversable object or array to use initial values. Below programs illust
2 min read
What is the difference between fopen() and fclose() functions in PHP ?
In this article, we are going to discuss the differences between fopen() and fclose() functions. fopen() function: This function helps users to open a file or a web URL. It mainly accepts two parameters - One is $file that specifies the file or an URL to be opened and the other is $mode that specifi
2 min read
PHP | AppendIterator __construct() Function
The AppendIterator::__construct() function is an inbuilt function in PHP which is used to construct an AppendIterator. Syntax: public AppendIterator::__construct( void ) Parameters: This function does not accept any parameters. Return Value: This function does not return any value. Below programs il
2 min read