To add to some of the already excellent answers:
Abstract classes let you provide some degree of implementation, interfaces are pure templates. An interface can only define functionality, it can never implement it.
Any class that implements the interface commits to implementing all the methods it defines or it must be declared abstract.
Interfaces can help to manage the fact that, like Java, PHP does not support multiple inheritance. A PHP class can only extend a single parent. However, you can make a class promise to implement as many interfaces as you want.
type: for each interface it implements, the class takes on the corresponding type. Because any class can implement an interface (or more interfaces), interfaces effectively join types that are otherwise unrelated.
a class can both extend a superclass and implement any number of interfaces:
class SubClass extends ParentClass implements Interface1, Interface2 {
// ...
}
Please explain when I should use an interface and when I should use abstract class?
Use an interface when you need to provide only a template with no implementation what so ever, and you want to make sure any class that implements that interface will have the same methods as any other class that implements it (at least).
Use an abstract class when you want to create a foundation for other objects (a partially built class). The class that extends your abstract class will use some properties or methods defined/implemented:
<?php
// interface
class X implements Y { } // this is saying that "X" agrees to speak language "Y" with your code.
// abstract class
class X extends Y { } // this is saying that "X" is going to complete the partial class "Y".
?>
How I can change my abstract class in to an interface?
Here is a simplified case/example. Take out any implementation details out. For example, change your abstract class from:
abstract class ClassToBuildUpon {
public function doSomething() {
echo 'Did something.';
}
}
to:
interface ClassToBuildUpon {
public function doSomething();
}