Constructor in Php


Definition of a Constructor
A constructor is a special function of a class that is automatically executed whenever an object of a class gets instantiated.

What does this all mean?
Lets revisit that definition in more simple terms. A constructor is a special function – this means that a constructor is a function; but its special. But, why is it special? It’s special because it is automatically executed or called when an object of a class is created.

Why do we need a Constructor?
It is needed as it provides an opportunity for doing necessary setup operations like initializing class variables, opening database connections or socket connections, etc. In simple terms, it is needed to setup the object before it can be used.

PHP5 Constructor
In PHP5 a constructor is defined by implementing the __construct() method. This naming style has been introduced in PHP5. In PHP4, the name of the constructor was the same name as that of the class. So, for example if you had a class Customer, you would have to implement a function Customer().
PHP5 to be backward complaint also supports the PHP4 rule. When an object is created, PHP5 searches for __construct() first. If __construct() is not defined it then searches for a method with the same that of the class. However, if you define both; PHP5 will first search for __construct() method and execute it if available, otherwise it will execute the same class name function.

Let’s look at how to define a PHP5 Constructor
class Customer {
 public function __construct() {
  //code
 }
}
Let’s look at a real example:
class Customer {
 private $first_name;
 private $last_name;
 private $outstanding_amount;
 
 public function __construct() {
  $first_name = "";
  $last_name = "";
  $outstanding_amount = 0;
 }
 
 public function setData($first_name, $last_name, $outstanding_amount) {
  $this->first_name = $first_name;
  $this->last_name = $last_name;
  $this->outstanding_amount = $outstanding_amount;
 }
 
 public function printData() {
  echo "Name : " . $first_name . " " . $this->last_name . "\n";
  echo "Outstanding Amount : " . $this->outstanding_amount . "\n";
 }
 
}
 
$c1 = new Customer();
$c1->setData("Hiren","Prajapati",0);
In the above example on line number 26, we create a new object of the Customer class. the ‘new’ operator is responsible for creating the Customer class. At this point PHP5 searches the Customer class to see if a constructor has been defined. Therefore, it calls the constructor method i.e. __construct() defined starting from line no 7. The __construct() method sets the $first_name and $last_name to blank and sets the $outstanding_amount to zero.
Parameterized Constructor or Argument Constructor
A parameterized or argument constructor is a constructor which accepts values in the form of arguments in the constructor. Unlike other programming languages where overloaded argument constructors is possible, in PHP5 you cannot overload constructors.
Example:
class Customer {
 private $first_name;
 private $last_name;
 private $outstanding_amount;
 
 public function __construct($first_name, $last_name, $outstanding_amount) {
  $this->setData($first_name, $last_name, $outstanding_amount);
 }
 
 public function setData($first_name, $last_name, $outstanding_amount) {
  $this->first_name = $first_name;
  $this->last_name = $last_name;
  $this->outstanding_amount = $outstanding_amount;
 }
 
 public function printData() {
  echo "Name : " . $first_name . " " . $this->last_name . "\n";
  echo "Outstanding Amount : " . $this->outstanding_amount . "\n";
 }
 
}
 
$c1 = new Customer("Hiren","Prajapati",0);
In the above example on line number 24, we create a new object $c1 and pass values “Hiren”, “Prajapati” and zero to the constructor defined on line number starting 7. The constructor now takes 3 arguments and stores them in the internal private variable $first_name, $last_name and $outstanding_amount respectively.
In the next tutorial you will learn about PHP5 Destructor.
Feel free to write comments if you need more examples or if you need to ask a question on PHP5 Class. You can also subscribe to my notification service to be informed as an when a new tutorial article goes online. Subscribe Below

No comments: