找回密码
 免费注册

php教程:析构方法

[复制链接]
发表于 2022-12-2 15:26:53 | 显示全部楼层 |阅读模式
php教程:析构方法

当对象销毁的时候自动调用
语法
  1. function __destruct(){
  2. }
复制代码
脚下留心:析构函数不可以带参数
例题
  1. <?php
  2. class Student {
  3.         private $name;
  4.         //构造方法
  5.         public function __construct($name) {
  6.                 $this->name=$name;
  7.                 echo "{$name}出生了<br>";
  8.         }
  9.         //析构方法
  10.         public function __destruct() {
  11.                 echo "{$this->name}销毁了<br>";
  12.         }
  13. }
  14. //测试
  15. $stu1=new Student('tom');
  16. $stu2=new Student('berry');
  17. $stu3=new Student('ketty');
  18. echo '<hr>';
复制代码

QQ|Archiver|小黑屋|网站地图|深圳市金黑网络技术有限公司 ( 粤ICP备2021124338号|粤公网安备 44030702003689号 )

GMT+8, 2026-8-8 17:14 , Processed in 0.057703 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表