Abstract Classes in PHP OOPS

 <?php


abstract class BaseEmployee
{
    protected $firstname;
    protected $lastname;

    public function getFullName()
    {
        return $this->firstname . " " . $this->lastname;
    }
   
    public abstract function getMonthlySalary();

    public function __construct($f, $l)
    {
        $this->firstname = $f;
        $this->lastname = $l;
    }
}

class FullTimeEmployee extends BaseEmployee
{
    protected $annualSalary;
   
    public function getMonthlySalary()
    {
        return $this->annualSalary / 12;
    }
}

class ContractEmployee extends BaseEmployee
{
    protected $hourlyRate;
    protected $totalHours;

    public function getMonthlySalary()
    {
        return $this->hourlyRate * $this->totalHours;
    }
}

$fulltime = new FullTimeEmployee("FullTime", "Employee");

$contract = new ContractEmployee("Contract", "Employee");

echo $fulltime->getFullName() . "<br>";
echo $contract->getFullName() . "<br>";

echo $fulltime->getMonthlySalary() . "<br>";
echo $contract->getMonthlySalary() . "<br>";

// OUTPUT :
//FullTime Employee
// Contract Employee
// 0
// 0

?>

Comments

Popular posts from this blog

GROUP BY Clause and HAVING Clause in PHP

Method Overriding in Traits in PHP

Mysqli database Connection and Display Table Data from Database