Defining and Using an Interface : Interface « Class « PHP






Defining and Using an Interface

 
<?php

interface Serializable {
  public function writeObject();
}

class Point implements Serializable {
  public $x;
  public $y;

  public function writeObject() {
    return serialize( $this );
  }
}

class Item implements Serializable {
  public function writeObject() {
    return serialize( $this );
  }
}

class Serializer {
  private $sArray = array();

  function register( Serializable $obj ) {
    $this->sArray[] = $obj;
  }

  function output() {
    foreach ( $this->sArray as $obj ) {
      print $obj->writeObject()."\n";
    }
  }
}

$serializer = new Serializer();
$serializer->register( new Item() );
$serializer->register( new Point() );
print "<pre>";
$serializer->output();
print "</pre>";
?>
  
  








Related examples in the same category

1.Implement an interface
2.Different class implements one interface
3.A Sample Interface
4.Implementing Multiple Interfaces
5.Defining an interface
6.Using Type Hinting with Interfaces