php中析构函数的作用是:开释内存,当对象被烧毁时析构函数就会被挪用。定义析构函数的语法花样为:【__destruct()】。php运用垃圾接纳机制,自动消灭不再运用的对象,纵然不运用unset函数,析构函数也会自动被挪用。
作用:
在对象被烧毁时析构函数被挪用,它的作用是开释内存。
定义析构函数的花样为:
__destruct()
举例:
class Preson{
public $name; //定义变量
public $age;
public $sex;
public $height;
function __construct($name,$age,$sex,$height){
$this->name = $name; //为变量赋值
$this->age = $age;
$this->sex = $sex;
$this->height = $height;
}
function __destruct(){
echo "对象被烧毁了";
}
}
$Preson1 = new Preson("明白","20","女","180");
echo $Preson1->name;
运转的效果为:
明白对象被烧毁了
运转完毕后,对象被烧毁了。
注重:
php运用的是一种“垃圾接纳”机制,自动消灭不再运用的对象,开释内存,就是说纵然不运用unset函数,析构要领也会自动被挪用。
假如您想进修更多相干学问,请接见ki4网。
以上就是php中析构函数的作用是什么的细致内容,更多请关注ki4网别的相干文章!
收藏 | 0