abstract classes vs interfaces in php

How do interfaces differ from abstract classes?

An abstract class is a class that does not have one or more methods implemented.

The interface is in essence an abstract class, in which all methods are not implemented, all methods are public and there can be no properties.

A class that contains at least one abstract method must be defined as abstract. But the abstract class may not contain abstract methods. Also it cannot create an instance of an abstract class. Methods that are declared abstract carry only descriptive meaning and cannot include implementations.

When inheriting from an abstract class, all methods marked abstract in the parent class must be defined in the descendant class (the scope of these methods must be the same or less strict).

The interface defines the methods that must be implemented, without defining them.

The abstract class is inherited (etxends), and the interface is implemented (implements). We can inherit only 1 class, and implement as many as you like.

The interface does not implement anything, and the abstract class implements only methods common to all descendants. An abstract class is needed when there are several classes that have a lot in common. Of course, you can use the interface, but then you will need to write a lot of identical code.

Leave a Reply

Your email address will not be published. Required fields are marked *