Skip to Content
PHPCODE
Abstract Classes in php
oops code / August 11, 2018

<?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();
echo $contract->getFullName();
echo $fulltime->getMonthlySalary();
echo $contract->getMonthlySalary();
?>

PHPCODE © 2024