Constructor in PHP, is a function which is called or invoked automatically when a class object is created. It is used to initialize data members while object creation.
__construct Function
To add constructor in class, PHP provides __construct function, it can accept any number of arguments.
function keyword than Double underscore and construct keyword without space and the () function symbol is used to create constructor.
Syntax
function __construct($argument1, $argument2,...){
}
Example
<?php
class Mobile{
// Properties
public $company;
// constructor
function __construct($company) {
$this->company = $company;
}
function get_name() {
return $this->$company;
}
}
Mobile myPhone = new Mobile("Motorola");
myPhone->get_name();
?>