Main difference between constructors in php

We are using OOPS(Object Oriented Programming System) in PHP because it is more flexible and reliable then Procedural Level language. In this section you will come to know how to define constructor in PHP.

Here are two way to define constructor, As you can see in the given example a class has been created with the name Student.

* Two Types to define the
* Constructor in the Class

class Student{

public $student_name;

#Main Mehod to invoke a Constractor
function __construct( $name ){
$this->student_name = $name;
$this->student_name .= " (Parent) ";

#Second Type to define a Constractor
function student( $name ){
$this->student_name = $name;
$this->student_name .= " (Sec) ";

function display(){
echo $this->student_name . " is in Class Student!";

$st = new Student('James');


As you can see in above example there are two constructor. The only difference between them is first one is by default constructor, in this you won’t have to rename the name of constructor if you want to change the class name.

On the other hand the second constructor is has to be defined with the name of Class, as well as you also have to rename the constructor name whenever you try to rename the Class name.

Note: In PHP(5+) version it will automatically assumed first one “__construct()” by default constructor if you want to include both constructor at same time.

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.