如果我有以下设置,我发现PHP5没有调用__destruct()函数:
class test {
__destruct()
{
echo 'hehe';
exit;
}
}
header('Location: http://test.com/');
exit;
它从不调用destruct函数
Pas*_*TIN 12
该析构函数被调用:
使用标头重定向不会阻止析构函数被调用.
另请注意,析构函数在PHP脚本的末尾调用 - 但不会阻止重定向,因为已经生成了标题"redirect".
例如,使用以下代码:
class Test {
public function __destruct() {
echo 'hehe';
file_put_contents('/tmp/test-desctructor.txt', "glop\n");
exit;
}
}
$a = new Test();
header('Location: http://example.com/');
exit;
Run Code Online (Sandbox Code Playgroud)
(注意我纠正了一些错误,并添加了类的实际实例)
您不会hehe在输出中看到" ",但您会发现该文件/tmp/test-desctructor.txt已创建:
$ cat /tmp/test-desctructor.txt
glop
Run Code Online (Sandbox Code Playgroud)
如果要hehe在输出上获得" ",则需要删除重定向.
在生成标头之后调用析构函数 - 并且从析构函数调用exit将不会更改已生成该标头的事实.
哦,这是手册中 的注释(引用 - 在页面底部):
注意:在脚本关闭期间调用的析构函数已经发送了HTTP头.
这就是为什么你没有看到你的" hehe"字符串:析构函数被调用; 你只是在屏幕上看不到它;-)
这就是为什么我在我的例子中使用了一个文件,btw ;-)
| 归档时间: |
|
| 查看次数: |
1375 次 |
| 最近记录: |