析构函数 (方法)(推荐学习:PHP编程从入门到精通)
__destruct ( void ) : void
PHP 5 引入了析构函数的概念,这类似于其它面向对象的语言,如 C++。析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。
析构函数示例
<?phpclass MyDestructableClass { function __construct() { print "In constructor\n"; $this->name = "MyDestructableClass"; } function __destru<b style="color:transparent">来源gao@dai!ma.com搞$代^码网</b>ct() { print "Destroying " . $this->name . "\n"; }}$obj = new MyDestructableClass();?>
析构函数即使在使用 exit() 终止脚本运行时也会被调用。在析构函数中调用 exit() 将会中止其余关闭操作的运行。
Note:
析构函数在脚本关闭时调用,此时所有的 HTTP 头信息已经发出。脚本关闭时的工作目录有可能和在 SAPI(如 apache)中时不同。
Note:
试图在析构函数(在脚本终止时被调用)中抛出一个异常会导致致命错误。
以上就是php析构方法怎么用的详细内容,更多请关注搞代码gaodaima其它相关文章!