PHP7 OOPs Abstract Class & Method

The abstract class is declared with the keyword abstract. This abstract class cannot be instantiated, it only inherited. An abstract class has at least one abstract method, preceeded with the abstract keyword and a semicolon at the end of the method and the abstract method does not have any opening and closing braces.

Example

<?php
abstract class Employee
{
    public $name;
    public $company;
    
    abstract public function detail();
    public function intro()
    {
        return $this->name . " works in a Company " . $this->company. ".";    
    }
    
}
class Corporate extends Employee
{
    public function detail()
    {
        return "Employee details are here -<br/>";    
    }
    public function intro()
    {
        return parent::intro();    
    }
}
$cor = new Corporate();
$cor->name = "John";
$cor->company = 'etp';
echo $cor->detail();
echo $cor->intro();
?>

Output

Employee details are here -
John works in a Company etp.

In the above example, we have seen that, an abstract class is declared with a keyword abstract and the function in the abstract class is also preceeded with abstract keyword.








Read more articles


General Knowledge



Learn Popular Language