SyntaxStudy
Sign Up
PHP Interface Syntax and Rules
PHP Intermediate 4 min read

Interface Syntax and Rules

Interface Syntax

Interface methods are public by default and must not have a body. Interfaces can define constants but not properties.

Example
<?php
interface Logger {
    // Constants are allowed
    const DEFAULT_LEVEL = "info";

    // Methods must be public, no body
    public function log(string $level, string $message): void;
    public function info(string $message): void;
    public function error(string $message): void;

    // Properties are NOT allowed in interfaces
    // public string $prefix; // Fatal error!
}
Pro Tip

Interface constants are inherited by implementing classes and can be accessed as ClassName::CONSTANT.