CKTIS / PHP
Keyword interface
Declare a contract without constructing objects of the interface itself.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
interface Named { public function label(): string; }Declare a contract without constructing objects of the interface itself. Its methods are public and have no implementation body. In PHP 8.4 an interface may also require public readable or writable properties. A plain public stored property can satisfy a required 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"]
Examples
<?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")));
Result
["ink","INK"]