Baza wiedzyCentrum egzaminacyjne
PLPolski
Wybierz język13

CKTIS / PHP

Słowo kluczowe implements

Wskazuje interfejsy realizowane przez klasę.

Wersja: PHP 8.4; repository runtime, production runtime not attested

Składnia

class Label implements PrintableLabel {}

Wskazuje interfejsy realizowane przez klasę. Konkretna klasa musi udostępniać zgodne publiczne metody, także odziedziczone. Może realizować kilka interfejsów naraz. Parametr o typie interfejsu przyjmuje instancję dowolnej realizującej go klasy.

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

Przykłady

<?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();

Wynik

AB 2

Źródła