PHP - Custom Type Interfaces


An interface groups a set of function declarations without implementing them.

It specifies the name, return type, and arguments, but would not implement the code.

Interfaces are different from abstract classes, since they cannot contain any implementation at all.

Abstract classes could mix both method definitions and implemented ones.

The interfaces state what a class can do, but not how it is done.

interface Customer { 
   public function getMonthlyFee(): float; 
   public function getAmountToBorrow(): int; 
   public function getType(): string; 

interface is defined with the keyword interface, and that its methods do not have the word abstract.

Interfaces cannot be instantiated, since their methods are not implemented.

The only thing you can do is to make a class to implement them.

Implementing an interface means implementing all the methods defined in it.

Using a class that implements an interface is like writing a contract.

You ensure that your class will always have the methods declared in the interface, regardless of the implementation.

To implement an interface

class Basic implements Customer { 
        public function getMonthlyFee(): float { 
            return 10.0; 

        public function getAmountToBorrow(): int { 
            return 10; 

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

Related Topics