当前位置: 首页 > 后端技术 > PHP

PHP的构造函数和析构函数

时间:2023-03-29 20:53:59 PHP

接触PHP十几年了,概念有点模糊。我写了一个类,居然把__construt写成了__constructor。我读了很长时间的问题。对应构造函数和析构函数,写程序验证析构函数确实会被销毁。write('aaa');$hello->write('bbb');$hello->write('ccc');echo($path);class你好{private$f=false;函数__construct($path){$this->f=fopen($path,'w');$this->write('*开始*');}函数__destruct(){$this->write('*end*');如果($this->f){fclose($this->f);}}publicfunctionwrite($something){if($this->f){fwrite($this->f,$something."\n");}}}生成的hello.txt内容如下:*begin*aaabbbccc*end*