pTutorial

New way to learn PHP

PHP 5 Constructor

Constructor

The constructor is a special purpose function that is automatically executed when instances of the class have been created. It uses to initialize property values or perform any action when the object is created.

Need of constructor

Constructor is basically used to initialize the data member of the class and perform such type of task that is needed to initialize at the time of object instantiation like database connection.

Ways of making a constructor method inside a class.

  • By using __construct()
  • By using same name of class

By using __construct() method

Syntax

function __construct()
{ 
echo "Constructor code here";
}

PHP 5 constructor example

<?php

class Student 
{
 
function __construct()
{
echo "PHP 5 constructor";	
}
}

$obj=new Student();


?>

Output

PHP 5 constructor

In the above example, when we create object of the Student class constructor will be executed.

Parameterized Constructor

A constructor which accept the value as parameter is called Parameterized Constructor.

Parameterized constructor example

<?php

class Student 
{

public $name;
public $branch;
 
function __construct($name1,$branch1)
{
$this-> name = $name1;

$this-> branch =$branch1;
}

public function display()
{
echo "The name of Student is " . $this-> name  ."
"; echo "The branch of Student is" . $this-> branch ."
"; } } $obj=new Student("John","Cse"); $obj->display(); ?>

Output

The name of Student is John
The branch of Student isCse

In the above example, when we create the object of the Student class data member name and roll is initialized with the given value at the time of object creation and call the display method with the help of object.

Download Source code


By using same name of class

We can also use the class name to make constructor, let's see the below example to understand the concept of constructor.

Example of constructor

<?php

class Student 
{
public $name;
function Student($name1)
{
$this-> name = $name1;
}
public function display()
{
return $this->name;
}

}
 
$obj=new Student("John");

echo " The name of Student is =";

echo $obj->display();

?>

Output

The name of Student is =John

Download Source code


Example of finding factorial by using constructor function

<?php

class fact
{ 
private $result = 1; 
private $number; 
function __construct($num) 
{ 
$this->number = $num; 
for($i=2; $i<=$num; $i++) 
{ 
$this->result *= $i; 
} 
}

 function display() 
{ 
echo "Factorial of {$this->number} is {$this->result}. "; 
}
}

$factorial = new Fact(5);

$factorial->display();

?>

Output

Factorial of 5 is 120.

Download Source Code

R

Constructor does not return anything.


Parent class constructor

Parent class constructor are not called automatically when child class constructor gets made.

If you want to execute parent class constructor, call to prent::__construct() in the child constructor.

Parent class constructor example

<?php

class Computer {
   function __construct() {
       print "In Computer class constructor
"; } } class Laptop extends Computer { function __construct() { parent::__construct(); print "In Laptop class constructor
"; } } // In Computer constructor //$obj = new Computer(); // In Laptop constructor $obj = new Laptop(); // In Computer constructor ?>

Output

In Computer class constructor
In Laptop class constructor

If you want to access the parent class constructor, use parent:: __constructor in your class, as shown above.


QUOTES OF THE DAY

Crafty men condemn studies, simple men admire them, and wise use them.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER