Skip to content

MIR0709

InterfaceInstantiation

An interface is used with new to create an instance. Interfaces cannot be instantiated; only concrete classes that implement the interface can be.

<?php
interface Serializable {
public function serialize(): string;
}
$obj = new Serializable(); // cannot instantiate interface

Instantiate a concrete class that implements the interface instead:

<?php
class JsonSerializable implements Serializable {
public function serialize(): string {
return '{}';
}
}
$obj = new JsonSerializable();