Home > Backend Development > PHP Tutorial > destruct 不对啊,其到底什么时候执行;

destruct 不对啊,其到底什么时候执行;

WBOY
Release: 2016-06-23 13:32:20
Original
984 people have browsed it

1.手册:
在脚本执行完毕,已经发送出PHP报头时,执行;

<?php class  Test{    public $a="---";        public function ccc(){        echo "1";    }        function __destruct(){        echo $this->a;    }    }$a = new Test();$a->ccc();$a = new Test();$a->a='===';$a->ccc(); ?> 
Copy after login


既然是脚本结束时,执行;
那为什么结果是: 1 --- 1 ===
而非 1 1 --- ===


回复讨论(解决方案)

__destruct 是析构函数,他在对象销毁时执行

当你在第18行 $a = new Test(); 时
第16 行 $a = new Test(); 的对象 $a 就被销毁了(因为赋了新值)
你把第 18 行改成 $a = 123; 看得就更清楚了

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template