Home >Backend Development >PHP Tutorial >Analyze the specific usage of PHP5 destructor_PHP tutorial

Analyze the specific usage of PHP5 destructor_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:29:111000browse

In the upgraded version of in PHP5, __construct() is used to name the constructor instead of having the same name as the class. The advantage of this is that the constructor can be made independent of Class name, when the class name changes, there is no need to modify the name of the constructor accordingly.

Contrary to the constructor, in PHP5, you can define a function named __destruct(), called the PHP5 destructor. PHP will call the destructor before the object is destroyed in memory. Let the object do some work before disappearing completely. Object destruction can generally be achieved by assigning a value to null.

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>/*  </SPAN><LI class=alt><SPAN> * Created on 2009-11-18  </SPAN><LI class=""><SPAN> *  </SPAN><LI class=alt><SPAN> * To change the template for this generated file go to  </SPAN><LI class=""><SPAN> * Window - Preferences - PHPeclipse - PHP - Code Templates  </SPAN><LI class=alt><SPAN> */  </SPAN><LI class=""><SPAN> class student{  </SPAN><LI class=alt><SPAN>  //属性  </SPAN><LI class=""><SPAN>  private $no;  </SPAN><LI class=alt><SPAN>  private $name;  </SPAN><LI class=""><SPAN>  private $gender;  </SPAN><LI class=alt><SPAN>  private $age;  </SPAN><LI class=""><SPAN>    </SPAN><LI class=alt><SPAN>  private static $</SPAN><SPAN class=attribute><FONT color=#ff0000>count</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>0</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>  function __construct($pname)  </SPAN><LI class=alt><SPAN>  {  </SPAN><LI class=""><SPAN>   $this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span class="attribute"><font color="#ff0000">name</font></span><span> = $pname;  </span>
</li>
<li class="alt"><span>   self::$count++;  </span></li>
<li class=""><span>  }  </span></li>
<li class="alt"><span>    </span></li>
<li class=""><span>  function __destruct()  </span></li>
<li class="alt"><span>  {  </span></li>
<li class=""><span>   self::$count--;  </span></li>
<li class="alt"><span>  }  </span></li>
<li class=""><span>    </span></li>
<li class="alt"><span>  static function get_count()  </span></li>
<li class=""><span>  {  </span></li>
<li class="alt"><span>   return self::$count;  </span></li>
<li class=""><span>  }  </span></li>
<li class="alt"><span> }  </span></li>
<li class=""><span>   </span></li>
<li class="alt">
<span> $</span><span class="attribute"><font color="#ff0000">s1</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">new</font></span><span> student("Tom");  </span>
</li>
<li class=""><span> print(student::get_count());  </span></li>
<li class="alt"><span>   </span></li>
<li class="">
<span> $</span><span class="attribute"><font color="#ff0000">s2</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">new</font></span><span> student("jerry");  </span>
</li>
<li class="alt"><span> print(student::get_count());  </span></li>
<li class=""><span>   </span></li>
<li class="alt">
<span> $</span><span class="attribute"><font color="#ff0000">s1</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">NULL</font></span><span>;  </span>
</li>
<li class=""><span> print(student::get_count());  </span></li>
<li class="alt"><span>   </span></li>
<li class="">
<span> $</span><span class="attribute"><font color="#ff0000">s2</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">NULL</font></span><span>;  </span>
</li>
<li class="alt"><span> print(student::get_count());  </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

The above code is the specific method of using the PHP5 destructor. I hope it will be helpful to everyone.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446387.htmlTechArticleIn the upgraded version of PHP5, __construct() is used to name the constructor instead of The class name has the same name. The advantage of this is that the constructor can be independent of the class name. When the class name changes,...
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