Constructor Functions in PHP

Constructor Functions in PHP are special type of functions which are called automatically whenever an object is created. So we take full advantage of this behaviour, by initializing many things through constructor functions.

PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.

Following example will create one constructor for Books class and it will initialize price and title for the book at the time of object creation.


function __construct( $par1, $par2 ) {
$this->title = $par1;
$this->price = $par2;
}

Now we don\’t need to call set function separately to set price and title. We can initialize these two member variables at the time of object creation only. Check following example below −


$physics = new Books( \"Physics for High School\", 10 );
$maths = new Books ( \"Advanced Chemistry\", 15 );
$chemistry = new Books (\"Algebra\", 7 );

/* Get those set values */
$physics->getTitle();
$chemistry->getTitle();
$maths->getTitle();

$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();

This will produce the following result −

Physics for High School
Advanced Chemistry
Algebra
10
15
7

Scroll to Top