CKTIS / PHP
Klíčové slovo implements
Uvádí rozhraní implementovaná třídou.
Verze: PHP 8.4; repository runtime, production runtime not attested
Syntaxe
class Label implements PrintableLabel {}Uvádí rozhraní implementovaná třídou. Konkrétní třída musí poskytovat kompatibilní veřejné metody, které mohou být i zděděné. Může implementovat více rozhraní. Parametr typu rozhraní přijme instanci kterékoli implementující třídy.
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
Příklady
<?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();
Výsledek
AB 2