Interface

Object interfaces allow to create code which specifies which methods a class must implement, without having to define how these methods are implemented.

Interfaces may have methods signatures, without a body, constants. Since version 8.4, they may also have properties, as long as the property is public, and the hooks are abstract, or without body.

<?php

    // Declare the interface 'Template'
    interface Template
    {
        public const A = 1;

        private string $p {
            get;
        }

        public function setVariable($name, $var);
        public function getHtml($template);
    }

    // Implement the interface
    class WorkingTemplate implements Template
    {
        private $vars = [];

        public function setVariable($name, $var)
        {
            $this->vars[$name] = $var;
        }

        public function getHtml($template)
        {
            foreach($this->vars as $name => $value) {
                $template = str_replace('{' . $name . '}', $value, $template);
            }

            return $template;
        }
    }

?>

Documentation

See also Interfaces vs Abstract Classes in PHP, Interfaces - the misunderstood concept, Granular interfaces, When to add an interface to a class and Code to an interface!.

Related : Fluent Interface, BackedEnum, Class Interface Trait Enumeration (CITE), Object, UnitEnum, Countable Interface, Expressive Interface, Flexibility, Polymorphism, Program To Interface, Direct Output, Interoperability, Intersection Type, Proxy, Facade, Proxy Class, SplObserver, SplSubject, Base Class, class_implements(), DateTimeInterface, Decorator Pattern, DOMChildNode, DOMParentNode, IteratorAggregate, RandomCryptoSafeEngine, RandomEngine, Reflector, SeekableIterator, SessionHandlerInterface, SessionIdInterface, SessionUpdateTimestampHandlerInterface, Dependency Inversion (DIP), Existential Type, Ports And Adapters, Row Polymorphism, First-Class Module, Pattern, Abstract Class, Structural Typing, Type Class