站长学院通行证

 

返回“站长学院”网站首页

 当前位置 → 中国站长学院网络编程Php编程 → 浏览正文


TAG:  PHP 教程 对象编程

PHP开发经典教程 (Part7):面向对象编程


作者:中国站长学院(整理)     来源:IT168     点击数:     更新时间:2007年11月19日

【字体: 】             【背景色 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白(默认色) 】    【收藏本文】   【打印


析构器 

    就像存在构造器一样,因此析构器也是存在的。析构器是一种对象方法,当对内存中的一个对象的最后引用被销毁时调用该对象方法,且它们通常被分派了清理工作的任务(例如,关闭数据库连接或者文件,销毁一个会话等等)。

析构器仅在PHP 5中可用,而且必须被命名为__destruct()。下面是一个对其进行说明的例子:

 

<?php // PHP 5 // class definition class Bear { // define properties public $name; public $weight; public $age; public $sex; public $colour; // constructor public function __construct() { $this->age = 0; $this->weight = 100; $this->colour = "brown"; } // destructor public function __destruct() { echo $this->name." is dead. He was ".$this->age."
years old and ".$this->weight." units heavy. Rest in peace! "; } // define methods public function eat($units) { echo $this->name." is eating ".$units." units of food... "; $this->weight += $units; } public function run() { echo $this->name." is running... "; } public function kill() { echo $this->name." is killing prey... "; } } // create instance of Bear() $daddy = new Bear; $daddy->name = "Daddy Bear"; $daddy->age = 10; $daddy->kill(); $daddy->eat(2000); $daddy->run(); $daddy->eat(100); ?>


    这里,一旦脚本执行结束,对于变量$daddy的引用就不再存在了,因此,析构器将被自动调用。程序的输出就像下面这样:

Daddy Bear is killing prey... Daddy Bear is eating 2000 units of food... Daddy Bear is running... Daddy Bear is eating 100 units of food... Daddy Bear is dead. He was 10 years old and 2200 units heavy. Rest in peace!

 

上一页      下一页
本文共 9 页,第  [1]  [2]  [3]  [4]  [5]  [6]  [7]  [8]  [9]  页


责任编辑:忧郁的秋天

注册投稿

收藏本文到  >>>

相关文章

phpcms被ku6网正式收购05/01 
模板教程【Pagetitle 标记】04/11 
模板教程【Prenext 标记】(十04/11 
模板教程【Pagebreak 标记】04/11 
模板教程【Pagelist 标记】(04/11 
模板教程【List 标记】(十一)04/11 
模板教程【Channelartlis04/11 
模板教程【Mynews 标记】(九)04/11 
模板教程【Flink 标记】(八)04/11 
模板教程【Vote 标记】(七)04/11 
模板教程【Mytag 标记】(六)04/11 
模板教程【Type 标记】(四)04/11 
模板教程【Channel 标记】(三04/11 
模板教程【Field 标记】(二)04/11 
模板教程【Arclist 标记使用实04/11 
DedeCMS模板制作使用实例教程序04/11 

推荐导读

热门资讯

资讯快报

论坛新贴

返回首页

关于我们 - 帮助(?) - 广告服务 - 诚征英才 - 友情链接 - 网站地图 - 站长BLOG

返回网站顶部

Copyright © 2007 中国站长学院(CNZZXY.COM). All Rights Reserved
站长学院服务资咨热线:0755-84289786    业务合作:(0)13554938953  (0)13423986856    网络宽带及主机提供:深圳傲网科技
粤ICP备08006968号   站长:忧郁的秋天(向管理员发邮件)   站长交流QQ群①:1815595   站长交流QQ群②:32960074