Cleaning Up with the __destruct Method (PHP 5 Only) : Destructors « Class « PHP






Cleaning Up with the __destruct Method (PHP 5 Only)

 
<?php

class 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 );
?>
  
  








Related examples in the same category

1.destuctor in action
2.Class destructors
3.Accessing instance-specific data within a destructor
4.Class with destructors
5.Defining an object destructor
6.Declaring and Using Object Constructors and Destructors