PHP: implements, abstract example
<?php
abstract class Automovel
{
// template method - design pattern
final public function viaja(IntegerObject $km)
{
$this->ligaAutomovel();
$this->encheTanque();
$this->anda($km);
$this->desligaAutomovel();
}
}
interface AutomovelInterface
{
public function ligaAutomovel();
public function encheTanque();
public function anda(IntegerObject $km); // Pode-se obrigar uma tipagem nos parâmetros também
public function desligaAutomovel();
}
class Carro
extends Automovel
implements AutomovelInterface
{
// não implemente viajar(). Ela já foi definida pela classe abstrata Automovel
// deve-se implementar os métodos de AutomovelInterface independente da ordem
}
// dependência
class IntegerObject
{
protected $int = 0;
public function __construct($int)
{
$this->int = (int) $int;
}
public function get()
{
return $this->int;
}
}