CKTIS / PHP
Kľúčové slovo implements
Uvádza rozhrania implementované triedou.
Verzia: PHP 8.4; repository runtime, production runtime not attested
Syntax
class Label implements PrintableLabel {}Uvádza rozhrania implementované triedou. Konkrétna trieda musí poskytovať kompatibilné verejné metódy, aj zdedené. Môže implementovať viac rozhraní. Parameter typu rozhrania prijme inštanciu ktorejkoľvek implementujúcej triedy.
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
Prí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ýsledok
AB 2