PHP Constructor

Last Updated: July 31, 2021

class constructor is a special member function of a class.

This member function is executed whenever you create new object from that class. So you can use the constructor to initialize the member properties of the class.

Name of the constructor is __construct() without returning any data.

PHP constructor with parameter

class Product {

  public $name;

  public function __construct($param) {
    $this->name = $param;

$product1 = new Product('iPhone 7');
echo $product1->name; // foobar