__construct
定義。<?php class yourclass{ public $name; public $age; function __construct($name,$age){ $this->name = $name; $this->age = $age; } function get($key){ return $this->$key; } } $test_1 = new yourclass('Tom',20); echo $test_1->get('name'); $test_2 = new yourclass('Jim',30); echo $test_2->get('age'); ?>執行以上程式的輸出結果為:
Tom 30
__desctruct
定義。<?php class yourclass{ public $name; public $age; function __construct($name,$age){ $this->name = $name; $this->age = $age; } function get($key){ return $this->$key; } function __destruct(){ echo "execute automatically"; } } $test_1 = new yourclass('Tom',20); echo $test_1->get('name'); echo $test_1->get('age'); ?>執行以上程式的輸出結果為:
Tom 20 execute automatically