KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / PHP

Sleutelwoord interface

Declareert een contract zonder eigen instanties.

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

Syntax

interface Named { public function label(): string; }

Declareert een contract zonder eigen instanties. Interfacemethoden zijn publiek en hebben geen implementatie. Sinds PHP 8.4 kan een interface publiek leesbare of schrijfbare eigenschappen eisen. Een gewone publieke eigenschap voldoet aan een vereiste getter.

interface Named {public string $name {get;} public function label():string;}
class Product implements Named {public function __construct(public string $name){} public function label():string{return strtoupper($this->name);}}
function readName(Named $p){return [$p->name,$p->label()];} echo json_encode(readName(new Product("ink")));
["ink","INK"]

Voorbeelden

<?php
interface Named {public string $name {get;} public function label():string;}
class Product implements Named {public function __construct(public string $name){} public function label():string{return strtoupper($this->name);}}
function readName(Named $p){return [$p->name,$p->label()];} echo json_encode(readName(new Product("ink")));

Resultaat

["ink","INK"]

Bronnen