CKTIS / PHP
Palavra-chave implements
Indica as interfaces implementadas por uma classe.
Versão: PHP 8.4; repository runtime, production runtime not attested
Sintaxe
class Label implements PrintableLabel {}Indica as interfaces implementadas por uma classe. Uma classe concreta deve fornecer métodos públicos compatíveis, inclusive herdados. Pode implementar várias interfaces. Um parâmetro do tipo da interface aceita instâncias de qualquer classe que a implemente.
interface Label {public function text():string;} interface Measured {public function size():int;}
class Tag implements Label,Measured {public function text():string{return "AB";} public function size():int{return strlen($this->text());}}
function show(Label $v){return $v->text();} $t=new Tag(); echo show($t)," ",$t->size();
AB 2
Exemplos
<?php
interface Label {public function text():string;} interface Measured {public function size():int;}
class Tag implements Label,Measured {public function text():string{return "AB";} public function size():int{return strlen($this->text());}}
function show(Label $v){return $v->text();} $t=new Tag(); echo show($t)," ",$t->size();
Resultado
AB 2