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

Ajax Insert Records in Database Tutorial in PHP

Displaying Database data using Class, Object, Constructor, Destructor in PHP MySql

SubQuery with EXISTS and NOT EXISTS in PHP