PHP tutorial- class constructor |
||||||||||||||
OOP in PHPClass constructorYou can initialize the data members of an object when it is created by using the constructor method. The name of constructor is the same as the name of the class. Example: <?php class Employee { //data member var $emp_id=""; var $emp_name=""; var $emp_sex=""; var $emp_salary=""; //constructor with parameters Function Employee($id,$name,$sex,$salary){ $this->emp_id=$id; $this->emp_name=$name; $this->emp_sex=$sex; $this->emp_salary=$salary; } //method members function setName($name){ $this->emp_name=$name; } function getName(){ return $this->emp_name; } } $emp=new Employee("e001","Dara Chan","Male",1500);//create object and initialize data members echo "ID:".$emp->emp_id."<br>"; echo "Name:".$emp->getName();//access method member ?> |
| |||||||||||||
|
||||||||||||||