Introduction

Since there have already been some object-oriented introductions around here, I decided to post about something that is not regularly discussed. As of the title, I’m gonna talk about abstract classes and interfaces. Usually, they are not commonly used in PHP. However, every self-respected programmer should know them mainly because:

It is an essential part of object-oriented programming
- Nothing much more to add.
OOP is pretty identical in most programming languages
- So as to learn or work in other languages under OOP, you need to have a good grasp of every concept in that style of programming
It’s mostly used in the development of big projects
- Especially if any third-party people have to modify your project

How it works?

I’ll make some pretty basic explanation because from what I’ve seen on the net when started learning this, everything explained there was presented in a very high level. Basically, speaking they are contracts that enable you to define the rules under which an application should be working. Abstract classes and interfaces don’t achieve functionality in any way. For example, in the interfaces you don’t have any loops so as to extract or returning information. Let’s put up an example: Say you have developed an audio player like Winamp. So you’d like your software to work with different kinds of plugins or additional things. Using abstract classes and interfaces you synchronize the code so as to allow the usage of third-party software to yours (the way the third-party person works with your software and respectively the way you work with his). When it comes to the plugins allowance to access your code, you set up an interface which will define what methods should the external software should have and implement them. For example, if you were to say that the plugin should have at least three methods so as to be able to interact with your software, you could define the following ones.

getInfo
- Which will return the information about the plugins author and usage
getSettings
- Which will determine the options and capabilities of the software
setSettings
- Which will apply the chosen/defined settings

Interfaces

So as to use them, we need to create them first. We do this with the keyword interface and then declare the name of it.

<?php  
interface IGetInfo{  
     public function getInfo();  
}  
?>  

We have visibility with the public scope and the method that should be implemented - getInfo();
Note: Since I already mentioned, interfaces and abstract classes don’t achieve functionality so any syntax like getInfo(){} is invalid.

Now let’s define setInfo();. Because it is setInfo(); we ought to actually set something so we define a parameter that should be accepted for implementation.

<?php  
interface IGetInfo{  
     public function getInfo();  
     public function setInfo($data);  
?>  

Now in order for the third-party person to make an implementation he could create a class and use our interfaces. In classes it is called extending and in interfaces - implementation.

<?php  
interface IGetInfo{  
     public function getInfo();  
     public function setInfo($data);  
}  
class User implements IGetInfo{  
     public function getInfo(){  
           return "Keeper";  
     }  

     public function setInfo($data){  
          // Do something  
     }  
}  
?>  

That’s the most basic example I could think of. When implementing, we us the keyword implements after that we choose the method/methods. Now, one class can extend only another one, while in interface we can implement more than one. Let’s see how this goes below:

<?php  
interface IGetInfo{  
     public function getInfo();  
     public function setInfo($data);  
}  
interface Antagonism extends IGetInfo{  
     public function foo();  
}  
class User implements Antagonism {  
     public function getInfo(){  
           return "Keeper";  
     }  

     public function setInfo($data){  
          // Do something  
     }  
}  
?>  

Abstract Classes

An abstract class is any class that has at least one abstract method inside of it. For example, I have this following class (look below).

<?php  
class User{  
     public function getInfo(){  

     }  
     abstract public function foo(){  

     }  
}  

?>  

If we were to execute that, we would get an error.

Fatal Error: Abstract function User::foo() cannot contain body in ...  

This occurs because as well as interfaces, abstract methods also cannot contain implementations. They can rather contain declarations. So whenever we define something as abstract, we cannot use {} because they indicate an action and we are simply not allowed to do that.

<?php  
class User{  
     public function getInfo(){  

     }  
     abstract public function foo()  
}  

?>  

Now upon this execution of the code (where we removed the action), we would still get another error stating that we cannot have an abstract method inside a class that is not defined itself as abstract.

Fatal Error: Class User contains 1 abstract method and must therefore be declared abstract or implement the remaining method (User::foo) in ...  

This means that whenever we declare something as abstract (in our case a method) inside a class, we make the whole class abstract. And this cannot happen inside the class, so we need to define it as abstract outside the class where we create it. Like so:

<?php  
abstract class User{  
     public function getInfo(){  

     }  
     abstract public function foo()  
}  

?>  

One of the main difference between interfaces and abstract classes is that we can have properties or actions here.

<?php  
abstract class User{  
     public $data=array();  
     public function getInfo(){  

     }  
     abstract public function foo()  
}  

?>  

As well as that, when we create an abstract class we cannot make an instance of it.

$Keeper=new User();  

The only way we could use an abstract class is to extend it from another one. For example:

class User1 extends Keeper{  
     public function foo(){  
         // Do something  
     }  
}  
$Infamous=new User1();  

To proof the concept let’s execute this code which would return a string:

<?php  
abstract class User{  
     public $data=array();  
     public function getInfo(){  
           return "Contributed to Antagonism and Infamous";  
     }  
     abstract public function foo();  
}  
class User1 extends Keeper{  
      public function foo(){  

     }  
}  
$Infamous=new User1();  
echo $a->getInfo();  

?>  

Conclusion

I intend to post a lot more on object-oriented programming in PHP. This by far is my first PHP tutorial but expect a lot more in future.