1.11デストラクタ
1.11.1はじめに
オブジェクトが破棄されると自動的に呼び出します
文法
function __destruct(){
}
フットケア:デストラクタパラメータはできません
例
<?php
class Student {
private $name;
//构造方法
public function __construct($name) {
$this->name=$name;
echo "{$name}出生了<br>";
}
//析构方法
public function __destruct() {
echo "{$this->name}销毁了<br>";
}
}
//测试
$stu1=new Student('tom');
$stu2=new Student('berry');
$stu3=new Student('ketty');
echo '<hr>';
業績
1.11.2コンピュータのメモリ管理
コンピュータのメモリ管理:FIFO、最後のアウト
FIFOメモリ管理は、一般に、等スパイク、チケットなど、ビジネス・ロジックで使用され
最後のアウトは、デフォルトのメモリ管理コンピュータであります
1.11.3質問
質問1
<?php
class Student {
private $name;
//构造方法
public function __construct($name) {
$this->name=$name;
echo "{$name}出生了<br>";
}
//析构方法
public function __destruct() {
echo "{$this->name}销毁了<br>";
}
}
//测试
$stu1=new Student('tom');
$stu2=new Student('berry');
$stu3=new Student('ketty');
unset($stu2);
echo '<hr>';
/*
tom出生了
berry出生了
ketty出生了
berry销毁了
ketty销毁了
tom销毁了
*/
質問2
<?php
class Student {
private $name;
//构造方法
public function __construct($name) {
$this->name=$name;
echo "{$name}出生了<br>";
}
//析构方法
public function __destruct() {
echo "{$this->name}销毁了<br>";
}
}
//测试
new Student('tom');
new Student('berry');
new Student('ketty');
/*
tom出生了
tom销毁了
berry出生了
berry销毁了
ketty出生了
ketty销毁了
*/
質問3
<?php
class Student {
private $name;
//构造方法
public function __construct($name) {
$this->name=$name;
echo "{$name}出生了<br>";
}
//析构方法
public function __destruct() {
echo "{$this->name}销毁了<br>";
}
}
//测试
$stu=new Student('tom');
$stu=new Student('berry');
$stu=new Student('ketty');
/*
tom出生了
berry出生了
tom销毁了
ketty出生了
berry销毁了
ketty销毁了
*/