班级抽象:
abstract class aClass { // Force extending class to define these methods abstract public function setVariable($name,$var); abstract public function getHtml($template); }
对象接口:
interface iClass { // Force impementing class to define these methods public function setVariable($name,$var); public function getHtml($template); }
可能:
class MyClass extends MyAbstract implements MyInterface1,MyInterface2,MyInterface3 { }
不可能:
class MyClass extends MyAbstract1,MyAbstract2 implements MyInterface { }
此外,接口不能实现.
可能:
abstract class MyClass { function doSomething() { echo "I can give this method an implementation as this is an abstract class"; } }
不可能:
interface MyClass { function doSomething() { echo "I can NOT give this method an implementation as this is an interface"; } }
An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface. To them,your interface is only incidental,something that have to add on to the their code to be able to use your package.
An abstract class,in contrast,provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is,code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java,a class can inherit from only one base class.
您还应该看一看this question – 达米恩关于界面如何成为合同的重要观点.