Cleaning Up With The __Destruct Method (Php 5 Only)

<?phpclass ItemUpdater {
  public function update( Item $item ) {
    print "updating.. ";
    print $item->name;
  }
}class Item {
  public $name = "item";
  private $updater;  public function setUpdater( ItemUpdater $update ) {
    $this->updater=$update;
  }
  function __destruct() {
    if ( ! empty( $this->updater )) {
      $this->updater->update( $this );
    }
  }
}$item = new Item();
$item->setUpdater( new ItemUpdater() ) ;
unset( $item );
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.