PHP - Custom Type Abstract classes

Introduction

You can extend only from one parent class each time.

An abstract class is a class that cannot be instantiated.

It ensures that its children are correctly implemented.

You declare a class as abstract with the keyword abstract, followed by the definition of a normal class.

You can create abstract methods without implementing them in the parent class.

The abstract methods are forced to be implemented by child class.

Abstract methods are defined with the keyword abstract.

 abstract class Customer { 
    abstract public function getMonthlyFee(); 
    abstract public function getAmountToBorrow(); 
    abstract public function getType(); 
 } 

class Basic extends Customer { 
    public function getMonthlyFee(): float { 
        return 5.0; 
    } 

    public function getAmountToBorrow(): int { 
        return 3; 
    } 

    public function getType(): string { 
        return 'Basic'; 
    } 
 } 
 class Premium extends Customer { 
    public function getMonthlyFee(): float { 
        return 10.0; 
    } 

    public function getAmountToBorrow(): int { 
        return 10; 
    } 

    public function getType(): string { 
        return 'Premium'; 
    } 
 } 

Related Topics