WissensdatenbankPrüfungszentrum
DEDeutsch
Sprache wählen13

CKTIS / PHP

Schlüsselwort interface

Beschreibt einen Vertrag ohne eigene Instanzen.

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

Syntax

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

Beschreibt einen Vertrag ohne eigene Instanzen. Interfacemethoden sind öffentlich und ohne Implementierungsrumpf. Seit PHP 8.4 kann ein Interface öffentlich lesbare oder schreibbare Eigenschaften fordern. Eine einfache öffentliche Eigenschaft erfüllt einen geforderten 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"]

Beispiele

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

Ergebnis

["ink","INK"]

Quellen