Skip to content

Instantly share code, notes, and snippets.

@gbmoretti
Created July 3, 2013 13:08
Show Gist options
  • Save gbmoretti/5917718 to your computer and use it in GitHub Desktop.
Save gbmoretti/5917718 to your computer and use it in GitHub Desktop.
PHP Interface vs. Ruby Duck Typing
class Cambio
def aumentaMarcha
@marcha += 1
end
def diminiuMarcha
@marcha -= 1
end
end
class Carro
def initialize(cambio)
raise Exception("Esse cambio nao funcio") unless cambio.respond_to(:aumentaMarcha)
end
end
<?php
interface Cambio {
private $marcha
public function aumentaMarcha();
public function diminuiMarcha();
public function podeAumentar();
public function podeDiminuir();
}
class Carro {
public function __construc(Cambio $cambio) {
$this->cambio = $cambio;
}
public function acelera() {
$this->cambio->aumentaMarcha();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment