Abstract and Interfaces are design to create a series of constrains.
Here the class fails to proper implement interface methods.
code
abstractclassmyAdapter{
private $id;
abstractfunctioninsert();
abstractfunctionupdate();
publicfunctionsave(){
// ...
}
}
classPDO_myAdapterextendsmyAdapter{
publicfunction__construct($dsn){
// ...
}
publicfunctioninsertAdapter(){
// wrong method name // it should be insert()
}
publicfunctionupdate(){
// ...
}
// Fatal error: Class PDO_myAdapter contains 1 abstract method ...// myAdapter::insert()
}
Interface
Interfaces specifies an API that a class must implement.
Interfaces contain no body.
Here the class method save() is not compatible with the interface.
code
interfacemyAdapter{
publicfunctioninsert();
publicfunctionupdate();
publicfunctionsave($name=null);
}
classPDO_AdapterimplementsmyAdapter{
publicfunctioninsert(){
// ...
}
publicfunctionupdate(){
// ...
}
publicfunctionsave($name){
// wrong
}
// Fatal error// must be compatible with myAdapter::save($name = NULL)
}
Extend
A class can only extend only one parent.
It can implement multiple interfaces.