PHP

Exposing a Common Interface

I have now set up a convenient product hierarchy that represents my initial set of products, namely those I maintain locally, and those from the navigation equipment partner. However, if I wanted to add a new partner (such as a manufacturer of boats), and my developers were too busy to sit down and write a new class in my object hierarchy, it would be nice if I could let them write their own and tell them what methods I want them to have for products.

This is done in PHP by declaring an interface. Basically, this is a way of creating a set of methods that people must implement if they want to be viewed as a member of the class of objects with the same interface.

You declare an interface with the interface keyword. Classes indicate that they want to implement an interface by using the implements keyword:

<?php
  //
  // prefixing the interface name with a capital I is
  // strictly a naming convention that I use.
  //
  interface IProduct
  {
    public function get_ProductID();
    public function get_Name();
    public function get_Description();
    public function get_PricePerUnit();
    public function check_product_in_stock($in_num_desired);
    public function ship_product_units($in_num_shipped);
  }
  abstract class Product implements IProduct
  {
    // etc.
  }
?>

By inheriting from IProduct, the Product class is committing either itself or one of its descendant classes to implementing all of the methods in IProduct. Thus, to force its two inheriting classes LocalProduct and NavigationPartnerProduct to implement the methods get_number_in_stock and ship_product_units, the Product class no longer needs to declare them as abstractit can simply omit them, which means that any subclass is obliged to implement them or receive an error. If I were to create a simple test class that inherits from Product, but doesn't implement them

<?php
  class TestProduct extends Product
  {
  }
?>

I would see the following errors when I tried to load the script with this new class:

Fatal error: Class TestProduct contains 2 abstract methods and
  must therefore be declared abstract
  (IProduct::check_product_in_stock,
   IProduct::ship_product_units) in
  /home/httpd/www/phpwebapps/src/itfs.php on line 10

The key in the previous error indicates that two methods from IProduct were not implemented.

This lets my boating manufacturer partner implement his own product-like class by implementing the IProduct interface, as follows:

<?php
  require('iproduct.inc');
  class SuperBoatsProduct implements IProduct
  {
    protected $boatid;
    protected $model_name;
    protected $model_features;
    protected $price;
    //
    // initialize a new instance of this class.
    //
    public function __construct
    (
      $in_boatid,
      $in_modelname,
      $in_modeldesc,
    $in_price
  )
  {
    $this->boatid = $in_boatid;
    $this->model_name = $in_modelname;
    $this->model_features = $in_modeldesc;
    $this->price = $in_price;
  }
  public function __destruct()
  {
    // nothing to do
  }
  //
  // this is final because I don't want people fiddling with it.
  //
  final public function get_ProductID()
  {
    return $this->boatid;
  }
  public function get_Name()
  {
    return $this->model_name;
  }
  public function get_Description()
  {
    return $this->model_features;
  }
  public function get_PricePerUnit()
  {
    return $this->price;
  }
  public function get_number_in_stock($in_num_desired)
  {
    // go to my local dbs and see how many I have
    // left.  return -1 on a failure of some sort.
  }
    public function ship_product_units($in_num_shipped)
    {
      // go to my local dbs and mark $in_number units
      // as no longer available.  TRUE == ok, FALSE == bad.
    }
  }
?>

The boat manufacturer now has objects that appear like my own products and can easily integrate into my web application without my having to write any code for them (apart from designing my system well in the first place). I can even mix the products into collections of other products, and they would behave the same.

by BrainBellupdated
Advertisement: