Interfaces are not intended for code reuse. They are intended for abstraction. They allow classes using the template to test the interface instead of the base class of the template. In this way, it separates the implementation from the interface declaration.
, - template, template . , , , iTemplate ( ).
public function foo(Template $template) {
public function foo(iTemplate $template) {
, . . . :
, . , :
interface iBird {
public function fly();
public function speak();
public function swim();
public function walk();
}
class Duck implements iBird {
public function fly() {
}
public function speak() {
}
public function swim() {
}
public function walk() {
}
}
class Turkey implements iBird {
public function fly() {
}
public function speak() {
}
public function swim() {
throw new Exception('Turkeys can not swim!');
}
public function walk() {
}
}
, , , walk(), , (, , DRY)...
, :
abstract class Bird implements iBird {
public function fly() {
}
abstract public function speak();
public function swim() {
}
public function walk() {
}
}
class Duck extends Bird {
public function speak() {
}
}
class Turkey extends Bird {
public function speak() {
}
public function swim() {
throw new Exception('Turkeys can not swim!');
}
}
, 3 ! speak(), ( ).
? , , . , ... , , NonSwimmingBird, Bird, . a NonFlyingBird ShortRangeBird...
, . , , . ? , . . , ? ... , " ". ( , , , . . , )...
, . 2 "" , , . , , , , ( , ). , DRY , . DRY, , , , . , ...