Defining And Using An Interface

<?phpinterface 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 "

“;
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.