destuctor in action : Destructors « Class « PHP






destuctor in action


<?php
class Person {
    protected $name;    
    private $age;    
    private $id;    

    function __construct( $name, $age ) {
        $this->name = $name;
        $this->age  = $age;
    }

    function setId( $id ) {
        $this->id = $id;
    }
    
    function __destruct() {
        if ( ! empty( $this->id ) ) {
            print "saving person\n";
        }
    }
}

$person = new Person( "Joe", 31 );
$person->setId( 111 );
unset( $person );

?>

           
       








Related examples in the same category

1.Class destructors
2.Accessing instance-specific data within a destructor
3.Class with destructors
4.Cleaning Up with the __destruct Method (PHP 5 Only)
5.Defining an object destructor
6.Declaring and Using Object Constructors and Destructors