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.
// abstract class
abstract class DataAccess { 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”.
// interfaceinterface 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.
$cDataAccess = new Data_Access;
It would throw the following error:
PHP Fatal error: Uncaught Error: Cannot instantiate abstract class Data_Access ...// 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:
PHP Fatal error: Class Customers contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Data_Interface::insertRecord)
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
- In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.
- 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.
- 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:
interface Interface_A { }
interface Interface_B { }
interface Interface_C { }
interface MyInterface extends Interface_A, Interface_B, Interface_C { }
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
:
class Class_A { }
abstract class MyAbstractClass extends Class_A {
}
No problem there, you CAN extend an abstract class with exactly one class but if you try to add one more:
class Class_A { }
class Class_B { }
abstract class MyAbstractClass extends Class_A, Class_B {
}
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;
public static function foo() {}
}abstract class MyAbstractClass {
public static $foo = null;
abstract static function foo() {} // error
}