PHP OOP Interfaces Explained

Last Updated: 24 Jul, 2024

What is an interface in PHP?

An Interface allows you to create programs that specifies which methods a class must implement, without defining how those methods are implemented. Interfaces are defined using the interface keyword and all the methods declared inside an interface must be public. When a class implements an interface it must implement all the methods defined by that interface. Any class that implements an interface must have the same behavior in terms of what should be called, how it should be called, and what should be returned.

Points to note:

  • An interface is defined using the interface keyword.
  • An interface just declares the method signature without defining their contents.
  • All methods declared inside an interface must be public, because this is nature of an interface.
  • Any class that implements an interface is called the Concrete Class, it must implements all the methods declared by that interface along with compatibe signatures.
  • An interface can and is able to maintain only abstract methods and follows the fully abstraction mechanism.
  • An interface can not have variables or properties .
  • Using an interface, we can achieve the multiple inheritance in PHP.
  • To implement an interface, a class need to use implements keyword.
  • A class can also implement multiple interfaces by separating them with a comma.
  • A class can also implement two interfaces which define a method with the same name, but method declaration in both interfaces must be identical.
  • An interface can also extend another interface similarliy to classes with the help of extends keyword.

Example 1: Sample example program that get and set Item class properties by implementing ItemInterface

<?php
interface ItemInterface
{
    public function setName(string $name): void;
    public function getName(): string;
    public function setPrice(float $price): void;
    public function getPrice(): float;
}
class Item implements ItemInterface
{
    protected $name;
    protected $price;

    public function __construct($name, $price)
    {
        $this->name = $name;
        $this->price = $price;
    }
    public function setName(string $name): void
    {
        $this->name = $name;
    }
    public function getName(): string
    {
        return $this->name;
    }
    public function setPrice(float $price): void
    {
        $this->price = $price;
    }
    public function getPrice(): float
    {
        return $this->price;
    }
}

$item_obj = new Item('Blue Shirt',1259.50);
$item = array(
    'name' => $item_obj->getName(),
    'price' => $item_obj->getPrice()
);
print_r($item);
/* OUTPUT
Array
(
    [name] => Blue Shirt
    [price] => 1259.5
)
*/
?>

Example 2: An interface can also extends another interface and may have constants

<?php
interface BrandInterface
{
    public const BRAND = 'XYZ';
    public function getBrand(): string;
}
interface ItemInterface extends BrandInterface
{
    public function setName(string $name): void;
    public function getName(): string;
    public function setPrice(float $price): void;
    public function getPrice(): float;
}
class Item implements ItemInterface
{
    protected $name;
    protected $price;

    public function __construct($name, $price)
    {
        $this->name = $name;
        $this->price = $price;
    }
    public function setName(string $name): void
    {
        $this->name = $name;
    }
    public function getName(): string
    {
        return $this->name;
    }
    public function setPrice(float $price): void
    {
        $this->price = $price;
    }
    public function getPrice(): float
    {
        return $this->price;
    }
    public function getBrand(): string
    {
        return Product::BRAND;
    }
}

$itemobj = new Item('Yellow T-Shirt', 2599);
$item = array(
    'name' => $itemobj->getName(),
    'price' => $itemobj->getPrice(),
    'brnad' => $itemobj->getBrand()
);
print_r($item);
/* OUTPUT
Array
(
    [name] => Yellow T-Shirt
    [price] => 2599
    [brnad] => XYZ
)
*/
?>

Example 3: A class can also implement multiple interfaces if required

<?php
interface ABCInterface
{
    public const CONSTANT_ABC = 'ABC';
    public function getABCConstant(): string;
}
interface XYZInterface
{
    public const CONSTANT_XYZ = 'XYZ';
    public function getXYZConstant(): string;
}
class ClassA implements ABCInterface, XYZInterface
{
    public function getABCConstant(): string
    {
        return ClassA::CONSTANT_ABC;
    }
    public function getXYZConstant(): string
    {
        return ClassA::CONSTANT_XYZ;
    }
    public function getConstants()
    {
        $constants = array(
            'abc_constant' => $this->getABCConstant(),
            'xyz_constant' => $this->getXYZConstant()
        );
        return $constants;
    }
}

$obj = new ClassA();
$constants = $obj->getConstants();
print_r($constants);
/* OUTPUT
Array
(
    [abc_constant] => ABC
    [xyz_constant] => XYZ
)
*/
?>

 

Thank You, Please Share.

Recommended Posts

IMAGE

PHP OOP Abstract Classes Explained

A PHP class that has minimum one abstract method is called as abstract class and cannot be instantiated by itself.

IMAGE

PHP Magic Methods Explained

Magic Methods are special types of methods in PHP that allows you to perform certain special actions. These methods start with a double underscore (__).

IMAGE

PHP OOP Traits Explained

Traits in PHP, are a medium of implementing code reuse and allow us to reuse sets of functions freely in several independent classes.