PHP Constructor

PHP Constructor is use to preset the value of variable , sessions and cookies.When a PHP class is called 1st time automatically class will run class constructor function.In other word we can say that constructor function is use to initialize the object of the class and default constructor will execute automatically when the object of the class is called 1st time.

Rules for constructor :
  • Constructor does not have return type
  • It has same name as the name of the class
  • It cannot be inherit
  • It used to allocate resources such as memory, to dynamic data member of a class
Types of constructor :

1. Default constructor :- Default constructor is those constructor which has no parameter . it will execute
by default when class is executed. There is only one default constructor in a class
2. Parametrized Constructor : those constructor which has different -2 parameter with different-2
data types is called parametrized constructor .one class can contain more than one parametrized
constructor
3. Copy Constructor :- it is use to copy the value of exiting object values. .
Example of parametrized constructor
class Customer { private $first1_name;
private $last1_name;
private $outstanding_amount;
public function __construct($first1_name, $last1_name, $outstanding_amount) {
$this->setData($first1_name, $last1_name, $outstanding_amount);
}
public function setData($first1_name, $last1_name, $outstanding_amount) {
$this->first1_name = $first1_name;
$this->last1_name = $last1_name;
$this->outstanding_amount = $outstanding_amount;
}
public function printData() {
echo “Name : ” . $first1_name . ” ” . $this->last1_name . “\n”;
echo “Outstanding Amount : ” . $this->outstanding_amount . “\n”;
}
}
$c1 = new Customer(“shipu”,”asha”,0);

Thanks to www.freedownloadplugin.com

Comments

Popular posts from this blog

8 Best CMSs for building a well structured social networking website

Website