This article mainly introduces simple examples of PHP initialization objects and destructors. Friends who need it can come and refer to it. I hope it will be helpful to everyone
Code As follows:
<?php /********************************************** * construct 对象初始化函数使用 * destruct 析构函数的使用 * $this 关键字的使用($this关键字是用来访问当前对象中的对象 属性 和对象 * 方法的 系统变量 ) * **********************************************/ header("Content-Type:text/html;charset=UTF-8"); class mypc{ public $name; public $type; function construct($name='',$type=''){ //初始化对象,将初始化值放在括号内 $this->name=$name; $this->type=$type; } function vod(){ return $this->name.$this->type.'播放电影'; } function game(){ return $this->name.$this->type.'玩游戏'; } /************************ * 当对象内部的操作执行完毕的时候, * destruct()被调用, * 然后对象所使用的内存被释放出来。规则:后进先出 **************************/ function destruct(){ echo "<br>==============".$this->name; } } $pc1 = new mypc('家用电脑','台式机'); echo $pc1->vod()."<br>"; //当$pc1=null;的时候是当前实例操作完成后直接释放出对象 $pic2 = new mypc('公司电脑','笔记本'); echo $pic2->game();
The above is the detailed content of About php: Simple example analysis of initialization objects and destructors. For more information, please follow other related articles on the PHP Chinese website!