KennisbankExamencentrum
NL · NLNederlands (Nederland)
Taal kiezen13

CKTIS / PHP

PHP-type static

Het retourtype static koppelt het resultaat aan de klasse waarlangs de methode wordt aangeroepen.

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

Syntax

public static function make(): static

Het retourtype static koppelt het resultaat aan de klasse waarlangs de methode wordt aangeroepen. Een geërfde fabrieksmethode met new static maakt zo een instantie van de concrete subklasse. Een gewone instantie van de basisklasse zou bij die aanroep het retourtype schenden. static is niet toegestaan als parametertype.

class Card { public static function make(): static { return new static(); } }
class Badge extends Card {}
echo json_encode([get_class(Card::make()), get_class(Badge::make())]);
["Card","Badge"]

Voorbeelden

<?php
class Card { public static function make(): static { return new static(); } }
class Badge extends Card {}
echo json_encode([get_class(Card::make()), get_class(Badge::make())]);

Resultaat

["Card","Badge"]

Bronnen