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.
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
)
*/
?>
A PHP class that has minimum one abstract method is called as abstract class and cannot be instantiated by itself.
Magic Methods are special types of methods in PHP that allows you to perform certain special actions. These methods start with a double underscore (__).
Traits in PHP, are a medium of implementing code reuse and allow us to reuse sets of functions freely in several independent classes.