Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword implements

List the interfaces for which a class provides a contract implementation.

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

Syntax

class Label implements PrintableLabel {}

List the interfaces for which a class provides a contract implementation. Concrete classes must supply compatible public methods, including those inherited from parents. A class can implement multiple interfaces. An interface-typed parameter accepts an instance of any implementing class.

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

Examples

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

Result

AB 2

Sources