PHP - Custom Type Interface Definition

Introduction

Interfaces declare a consistent set of methods that classes must implement.

A class implements an interface.

At the same time, the class can also extend a parent class.

A class can implement more than one interface at once.

You create an interface via keyword interface rather than class.

You then specify a list of methods that implementing classes must include:

interface MyInterface {
           public function myMethod1($param1, $param2);
           public function myMethod2($param1, $param2);
}

Interfaces can't contain properties; they can only contain method declarations which can't contain any implementation code.

All methods in an interface must be public.

You can then make a class implement an interface using the implements keyword:


class MyClass implements MyInterface {
          public function myMethod1($param1, $param2) {
            // (implement the method here)
          }

          public function myMethod2($param1, $param2) {
            // (implement the method here)
          }
}

To implement more than one interface at once, separate the interface names with commas:

class MyClass implements MyInterface1, MyInterface2 {

The following example shows how to create and use a Product interface.

Demo

<?php

        interface Product {
          public function addStock($numItems);
          public function sellItem();
          public function getStockLevel();
        }/*from   w w  w . j av  a 2 s .  com*/

        class Phone implements Product {
          private $_screenSize;
          private $_stockLevel;

          public function getScreenSize() {
            return $this->_screenSize;
          }
           public function setScreenSize($screenSize) {
             $this->_screenSize = $screenSize;
           }

           public function addStock($numItems) {
             $this->_stockLevel += $numItems;
           }

           public function sellItem() {
             if ($this->_stockLevel  >  0) {
               $this->_stockLevel--;
               return true;
             } else {
               return false;
             }
           }

           public function getStockLevel() {
             return $this->_stockLevel;
           }
         }

         class PC implements Product {
           private $_color;
           private $_left;

           public function getColor() {
             return $this->_color;
           }

           public function setColor($color) {
             $this->_color = $color;
           }

           public function addStock($numItems) {
             $this->_left += $numItems;
           }

           public function sellItem() {
             if ($this->_left  >  0) {
               $this->_left--;
               return true;
             } else {
               return false;
             }
           }

           public function getStockLevel() {
             return $this->_left;
           }
         }

class StoreManager {
  private $_productList = array();
  public function addProduct(Product $product) {
    $this->_productList[] = $product;
  }

  public function stockUp() {
    foreach ($this->_productList as $product) {
      $product->addStock(100);
    }
  }
}

$phone = new Phone;
$phone->setScreenSize(42);
$pc = new PC;
$pc->setColor("yellow");
$manager = new StoreManager();
$manager->addProduct($phone);
$manager->addProduct($pc);
$manager->stockUp();
echo "There are". $phone->getStockLevel()."". $phone->getScreenSize();
echo "-inch televisions and". $pc->getStockLevel()."".
$pc->getColor();
$phone->sellItem();
echo "Selling...\n";
$pc->sellItem();
$pc->sellItem();
echo "There are now". $phone->getStockLevel()."". $phone->getScreenSize();
echo "-inch televisions and". $pc->getStockLevel()."".
$pc->getColor();
echo "tennis balls.\n";
?>

Result

Related Topic