Abstract vs Interfaces Classes in PHP

The Abstract Class

An abstract class is a class that has at least one abstract method. An abstract method is a method that is declared, but not implemented in the code.The class that inherit this abstract class need to define that method.There must be an abstract keyword that must be returned before this class for it to be an abstract class. Thus, we cannot create objects out of abstract classes. Instead, we need to create child classes that add the code into the bodies of the methods, and use these child classes to create objects. There can be more than one methods that can be left undefined.

  public $varAbstract;
protected $varHost;
// this abstract method confirm need to define in child class
abstract function db_connect();
protected function getResultArray(){
}
}

The Interface

Think of an interface as a template or contract for your concrete classes.The class that is fully abstract is called an interface. Any class that implements this interface must use implements keyword and all the methods that are declared in the class must be defined here. otherwise, this class also needs to be defined as abstract.When one or more classes use the same interface, it is referred to as “polymorphism”.

interface class DataInterface {
public function insertData($varDataRecord);
public function deleteData($varRecordId);
}interface class DataFilterInterface {
public function getList($varFilter);
}

Implementation of Abstract and interface

If we were to try to instantiate the Data_Access class as a standalone object:

Thus, we cannot create objects out of abstract classes. Instead, we need to create child classes that add the code into the bodies of the methods, and use these child classes to create objects.

It would throw the following error:

// Concrete class extend abstract and implement interfacesclass Customers extend DataAccess implements DataInterface,DataFilterInterface {public function __construct(){
$this->db_connect();
}// inherited interface class
public function insertData($varDataRecord){
}
// inherited interface class
public function deleteData($varRecordId){
}
// inherited interface class
public function getList($varFilter){
}
}

Because the interface enforces its implementation rule, if I tried to leave out one of the functions in Customers, for example insertRecord, and tried to instantiate it, we would receive a fatal error:

Difference between Abstract and interface

PHP abstract classes and interface are similar to like in other oops languages
the main differences in programming point of view are

  1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.
  2. Method of php interface must be public only can’t have access modifier by default everything assumed as public. Method in abstract class in php could be public or protected both.
  3. Interface support multiple inheritance but abstract class doses not support multiple inheritance

It means you can extend an interface with one or more (hence multiple inheritance) interfaces like:

As can be seen, we are extending MyInterface with three other interfaces Interface_A, Interface_A and Interface_C.

Let’s now try to extend an abstract class:

No problem there, you CAN extend an abstract class with exactly one class but if you try to add one more:

This time PHP would give you strange error without telling you what you are doing wrong:

Parse error: syntax error, unexpected ',', expecting '{'

I wish PHP would have given message somewhat like (hope PHP gives smarter error messages in future versions):

Fatal Error: You cannot extend an abstract class with more than one classes

4. An interface contains only incomplete member but abstract incomplete (abstract) and complete member also.

5. Interface does’n contain constructor but abstract can contain constructor

6. Member of interface can’t be static in abstract only complete member can be static

abstract class MyAbstractClass {     
public static $foo = null;
abstract static function foo() {} // error
}

Full Stack Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store