How to code constructors and destructors

How to code a constructor method

The syntax

public function __construct([parameterList]) { 
  // Statements to execute 
}

The default constructor

public function __construct() { }

The constructor for the Category class

public function __construct($id, $name) { 
  $this->id = $id; 
  $this->name = $name; 
}

The constructor for the Category class with default values

public function __construct($id = NULL, $name = NULL) { 
  $this->id = $id; 
  $this->name = $name; 
}

How to code a destructor method

The syntax

public function __destruct() { 
   // Statements to execute 
}

A destructor for a database class

public function __destruct() { 
  $this->dbConnection->close();
}

Description

Back