Home  >  Article  >  Backend Development  >  About php: Simple example analysis of initialization objects and destructors

About php: Simple example analysis of initialization objects and destructors

黄舟
黄舟Original
2017-07-02 10:34:221336browse

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=&#39;&#39;,$type=&#39;&#39;){ //初始化对象,将初始化值放在括号内
   $this->name=$name;
   $this->type=$type;
  }
  function vod(){
   
return
 $this->name.$this->type.&#39;播放电影&#39;;
  }
  function game(){
   return $this->name.$this->type.&#39;玩游戏&#39;;
  }
  /************************
  * 当对象内部的操作执行完毕的时候,
  * destruct()被调用,
  * 然后对象所使用的内存被释放出来。规则:后进先出
  **************************/
  function destruct(){
   echo "<br>==============".$this->name;
  }
 }
 $pc1 = new mypc(&#39;家用电脑&#39;,&#39;台式机&#39;);
 echo $pc1->vod()."<br>";
 //当$pc1=null;的时候是当前实例操作完成后直接释放出对象
 $pic2 = new mypc(&#39;公司电脑&#39;,&#39;笔记本&#39;);
 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn