PHP5 Tutorial- Definition of 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?
Actually 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
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(“Stuart”,”Broad”,0);
In the above example, 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. 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:
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(“Stuart”,”Broad”,0);
In the above example, we create a new object $c1 and pass values “Stuart”, “Broad” and zero to the constructor. The constructor now takes 3 arguments and stores them in the internal private variable $first_name, $last_name and $outstanding_amount respectively.
Follow @phpzag
