PHP- object (destructor) for

1.11 destructor

1.11.1 Introduction

When the object is destroyed automatically call

grammar

function __destruct(){
}

Foot care: destructor parameters can not

example

<?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>';

operation result

Here Insert Picture Description

1.11.2 Computer Memory Management

Computer Memory management: FIFO, last-out

FIFO memory management is generally used in the business logic, such as spike, tickets, etc.

Here Insert Picture Description

Last out is the default memory management computer

Here Insert Picture Description

1.11.3 Questions

Questions 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销毁了
*/

Questions 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销毁了
*/

Questions 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销毁了
*/
Released 1891 original articles · won praise 2010 · Views 180,000 +

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/105138724