BIT301 ยท 2082 ยท 5 marks
Write a program to create a class Employee with some attributes and methods. Create an object of the class and display some output using any method in the class.
Write a program to create a class Employee with some attributes and methods. Create an object of the class and display some output using any method in the class. [5]
Quick answer
A class is the blueprint that describes what an employee record holds and what can be done with it, and an object is one concrete employee built from that blueprint. In PHP the properties are declared with a visibility keyword, the constructor is the magic method __construct(), and inside any method the current object is referred to as $this, with -> used to reach a property or call a method.
An Employee class in PHP
<?php
class Employee
{
private int $id;
private string $name;
private string $department;
private float $salary; // monthly salary
public function __construct(int $id, string $name, string $department, float $salary)
{
$this->id = $id;
$this->name = $name;
$this->department = $department;
$this->salary = $salary;
}
public function getName(): string
{
return $this->name;
}
public function annualSalary(): float
{
return $this->salary * 12;
}
public function giveRaise(float $percent): void
{
$this->salary += ($this->salary * $percent) / 100;
}
public function display(): string
{
// the name and department are stored values that end up inside an HTML page,
// so they are escaped here, since a value held in the database may contain
// characters such as < or & that would otherwise be read as markup
$safeName = htmlspecialchars($this->name, ENT_QUOTES, "UTF-8");
$safeDept = htmlspecialchars($this->department, ENT_QUOTES, "UTF-8");
return "<p>Employee ID: " . $this->id . " "
. "Name: " . $safeName . " "
. "Department: " . $safeDept . " "
. "Monthly salary: Rs. " . number_format($this->salary, 2) . "</p>";
}
}
// creating an object of the class
$emp = new Employee(101, "Ram Sharma", "IT", 50000);
echo $emp->display();
echo "<p>Annual salary: Rs. " . number_format($emp->annualSalary(), 2) . "</p>";
$emp->giveRaise(10);
echo "<p>After a 10 percent raise:</p>";
echo $emp->display();
?>
The page produced is:
Employee ID: 101
Name: Ram Sharma
Department: IT
Monthly salary: Rs. 50,000.00
Annual salary: Rs. 600,000.00
After a 10 percent raise:
Employee ID: 101
Name: Ram Sharma
Department: IT
Monthly salary: Rs. 55,000.00
The parts of the class
| Part | Role |
|---|---|
class Employee { ... } | Declares the blueprint |
private int $id and the other three | The attributes, hidden from outside code |
__construct() | Runs automatically at new and fills the attributes |
$this | The object the method is currently working on |
annualSalary(), giveRaise(), display() | The methods, the behaviour of the object |
new Employee(101, "Ram Sharma", "IT", 50000) | Creates one object from the blueprint |
Declaring the attributes private is what makes giveRaise() worth writing: outside code cannot reach in and set an arbitrary salary, it must go through a method that applies the rule. PHP 8 allows the same class to be written more briefly with constructor property promotion, where public function __construct(private int $id, private string $name) declares and assigns the properties in one line.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.