Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

PHP type static

A static return type is tied to the class through which the method is called.

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

Syntax

public static function make(): static

A static return type is tied to the class through which the method is called. An inherited factory can therefore preserve the concrete subclass when it constructs new static. Returning an instance of the base class alone would violate a subclass call’s return contract. This form is a return type, not a parameter type.

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"]

Examples

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

Result

["Card","Badge"]

Sources