Maison >développement back-end >tutoriel php >解析PHP5析构函数的具体使用方法_PHP教程
在升级版的在PHP5中,则使用__construct()来命名构造函数,而不再是与类同名,这样做的好处是可以使构造函数独立于类名,当类名改变时,不需要在相应的去修改构造函数的名称。
与构造函数相反,在PHP5中,可以定义一个名为__destruct()的函数,称之为PHP5析构函数,PHP将在对象在内存中被销毁前调用析构函数,使对象在彻底消失之前完成一些工作。对象在销毁一般可以通过赋值为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> <li class=""><span>/* </span></li> <li class="alt"><span> * Created on 2009-11-18 </span></li> <li class=""><span> * </span></li> <li class="alt"><span> * To change the template for this generated file go to </span></li> <li class=""><span> * Window - Preferences - PHPeclipse - PHP - Code Templates </span></li> <li class="alt"><span> */ </span></li> <li class=""><span> class student{ </span></li> <li class="alt"><span> //属性 </span></li> <li class=""><span> private $no; </span></li> <li class="alt"><span> private $name; </span></li> <li class=""><span> private $gender; </span></li> <li class="alt"><span> private $age; </span></li> <li class=""><span> </span></li> <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> </li> <li class=""><span> function __construct($pname) </span></li> <li class="alt"><span> { </span></li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></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>
上面这段代码就是PHP5析构函数的具体使用方法,希望对大家有所帮助。