PHP: setters and getters
/*
* PHP: Using setters and getters
* This clsss will show the usage of PHP setters and getters
*
*/
* PHP: Using setters and getters
* This clsss will show the usage of PHP setters and getters
*
*/
class Employee
{
//This is private, so this can't be accessed from out side the class.
private $name = "Dantha Elvitigala";
//public function.this can be access from outside the class
//it's used to set the employee name into variable $name
public function setName($name){
$this->name= $name;
}
//public function.this also can be access from outside the class
//this use for get the emploee name stored in $name 
public function getName(){
return $this -> name;
}
}//end class
//create an instant of the Employee class
$employee = new Employee();
//set $name with another employee name
$employee -> setName("Chandika Elvitigala");
// printout the employee name
echo $employee->getName();
Comments
Post a Comment