CKTIS / PHP
Mot-clé implements
Indique les interfaces implémentées par une classe.
Version : PHP 8.4; repository runtime, production runtime not attested
Syntaxe
class Label implements PrintableLabel {}Indique les interfaces implémentées par une classe. Une classe concrète doit fournir les méthodes publiques compatibles, y compris par héritage. Elle peut implémenter plusieurs interfaces. Un paramètre typé par l’interface accepte toute instance qui l’implémente.
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
Exemples
<?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();
Résultat
AB 2